Hibernate - derived java classes for tables
I generated mapping files and POJOs in Netbeans instead of writing them myself. Is it possible to use a derived class in a place of an inherited class? An example would be something like this:
Person.hbm.xml - mapping file
Person.java - generated class (strategy class per table)
PersonExtended - class that extends Person.java
So when I create a new object:
PersonExtended personextended = new PersonExtended(<parameters>);
Would I be able to call methods like:
session.save(personextended) or session.delete(personextended)
?
Is this scenario sensible or should I add any code that I need to in a generated class? Thanks in advance for help or suggestions.
-------Edit--------
In my database I don't have the typical structure that would be possible to be mapped as an inheritance. I merely want to keep the additional methods开发者_运维技巧 separate from the main java class for an entity.
Best Regards, sass.
you will have to tell hibernate how your extended classes should be mapped via a the hbm.xml file. Depending on the strategy Hibernate should use for polymorphism you might have to assign a descriminator value. there are 3 different strategies when using subclasses known as "table per class" "table per concrete class" and "table per subclass".
You can define subclasses in the hbm-xml file by using the <subclass>
or <joined-subclass>
elements
if you correctly defined your hbm.xml file you can then use session.save(new PersonExtended())
or sth.
you can read up on this here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html
hope that helped..
精彩评论