views:

177

answers:

1

I have a class ClassA mapped to TableA. I also have a mapping for it and it has an entity-name of EntityA.

Now, I need to create another mapping named EntityATwo between ClassA for TableA, but slightly different.

Although I could copy-paste the mapping of EntityA to EntityATwo, it would be very difficult to maintain that.

Thus, my question is, how do I create the mapping for EntityATwo wherein I declare there only what is different between EntityATwo and EntityA, while the rest of the mappings are derived from EntityA.

Thanks, Franz

+1  A: 

I don't know a way to do that directly. But with your permission, I might elaborate.


In our code, we usually like to have one java class map to one semantic (we prefer to work with compile-checked class than unchecked Strings). Therefore, instead of mapping twice the class to the table with different entity names, we would map two classes. We would have :

  • ClassA as the common superclass, with most mapping that is common. It is declared (in hbm.xml) with all this mapping.
  • ClassA1 extends ClassA, provide its unique mapping (and java code if needed, otherwise it could be empty). It inherits the common mapping from ClassA.
  • ClassA2 also

You don't have duplication anymore. And you gain something on semantics.


Another possibility is to use annotations on the java class, only for the common mapping. In Hibernate, Annotations can be overriden (or completed) by xml mappings. So I guess you could override only the relevant part in xml, and you would have no duplication.

KLE
Please feel free to elaborate some more :-) I guess it's ok for me to have two classes mapped to the same table. But still, how do I say that EntityATwo map is practically the same as that of EntityA map with some slight differences.Thanks
Franz See
@Franz I edited to focus on your comment. Is it clearer? Hibernate site is down, so I can't provide a link.
KLE
Franz See
I believe Hibernate understands this already. Because of the inheritance in your classes (classes that are declared to Hibernate), it should understand this. I'm doing this routinely using Annotation technology (which I suggest if you can by the way), but I believe it should be the same for xml. Both mappings get translated to the same meta-data. (Note: we are using two-sides reference between some entities with annotations and other in xml, and it works fine!).
KLE

related questions