Hibernate - derived java classes for tables - java

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..

Related

hibernate cast base class into subclass

Ok I got a base class and a subclass
what I want to do is to turn base class into sub class in sql table
By changing discriminator value inside xml at run time without using annotation
how can I achieve such an approach
I'm using shared table per subclasses
We can do the Hibernate Inhertitance with and without using annotation
Take a look at this article
1.Table Per Hierarchy using xml file
2.Table Per Concrete class using xml file
3.Table Per Subclass using xml file

What's the difference between a Home object and a DAO?

In a Wicket/Spring/Hibernate project I inherited I find the following pattern:
For each Entity there exists an abstract class called EntityHome generated by Hibernate Tools which provides methods for finding, persisting, merging, and removing Entity. In another package there are classes called EntityDao for each EntityHome which in most cases simply extend EntityHome without adding any additional functionality.
Normally I would create a single generic DAO for handling persist, find, merge, and remove generically and have all DAOs extend this one.
The closest thing to some kind of documentation for Home Objects I found is http://docs.jboss.org/seam/1.1GA/reference/en/html/framework.html#d0e6756 and what I read there pretty much matches what a DAO should do.
So my question is: what is the difference between a Home Object and a DAO? Is there any at all?

Mapping one Entity into two different tables using annotations

I have a lot of trouble with the following problem.
I have an Entity "Home" which I use at two different locations within my code.
The problem is, that it is necessary to map this entity to different tables depending
on the class they were used in.
If we assume I would have the two classes Class1 and Class2. Both classes have the
an attribute of the "Home" type. Now I want that of attribute within class1 is mapped to the Table "CLASS1_HOME" and the attribute of class2 is mapped to the table "CLASS2_HOME".
I know that this is possible by e.g., using "MappedSuperClass" for the class "Home" and create a further class "Second_Home" which inherits all attributes from class "home". But my Question is if there is a possibilty to this without inheritance, because the attributes of home will not change and I think the "inheritance" solution is a kind of "dirty". Moreover, I want to this only by using annotations.
Is there a way to do it as I described it ?
If you don't want to use inheritance, and if you are saving HOME the same way in both CLASS1_HOME and CLASS2_HOME table, I do suggest something like this:
public class Home{
boolean Class1;
}
in this case when you persist it you can simply figure out if this is for Class1 or not. And if you want, based on the flag persisted in your database, you can create two views of CLASS1_Home and CLASS2_Home

Hibernate: How can I join 2 classes in one table?

So, I'm pretty new to Hibernate and I have a problem.
I have an abstract class (the super-class, class Super), and 5 subclasses which should use the proprieties from the class Super and add a new propriety (a new column)
So how can I do this? Should I extend the class Super from java, or it's enough to join the classes using a JPA annotation.
Here is the second problem. How can I have 1 table for 2 classes.
Someone (smarter than me) told me to use the #JoinTable, but form my searches with google, I think I need to use #Inheritance(strategy=InheritanceStrategy.JOINED)
Can I use the #JoinTable too?
Yours is a case of inheritance:
add the #Inheritance(stretegy=InheritanceStrategy.SINGLE_TABLE) annotation on your Super
add the #DiscriminatorColumn annotation (and setting its attributes name and discriminatorType) (again on the Super)
on each subclass extend the Super, and add the annotation #DiscriminatorValue, with different value for each of the subclasses.
If you are new to Hibernate, you should read its documentation. Inheritance strategies are explained here and using annotations to express inheritance strategy is explained here

Class inheritance with Hibernate and hbm2java

I am generating my domain objects from my hbm files with hbm2java and now I want them to all inherit a base class. This class will have some utility methods for dealing with listeners. It does not really need to be persisted and I am hoping that I would not have to do a hbm file for it since it will be abstract and only have methods. Is there a meta tag or something to have all generated class extend another class?
Or is this such a lame design that it simply cannot be done with hibernate?
I remember doing this way back. See this: http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/#d0e4159
Basically you want to use a meta tag here with attribute extends for a common base class or implements for a common base interface.

Categories