views:

120

answers:

1

Hey there!

I'm having a little problem setting up my webshop project. Thing is, I have a User() superclass and two subclasses, PrivateUser and BusinessUser.

Now, I'm not quite sure how to get my head around storing this relationship via hibernate.

For the purpose of this question, the User() class contains only one field:
String address;

the PrivateUser contains:
String firstName;

and the BusinessUser contains:
String CompanyName;

Each field has its getter and setter. As is right now, I would only store and be able to get firstName and companyName. When I fetch a user from my DB using Hibernate I would get a PrivateUser/BusinessUser with a null address.

Bottom line is, could someone point me towards a useful tutorial or better yet show a similar example code?

Thanks!

+1  A: 

The Hibernate online documentation has some very good information on mapping your classes, in particular this section on setting up subclasses:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-subclass

You want to pay special attention to using discriminator values and such if you are storing your sub-classed objects in a common table.

You might also consider picking up a copy of Java Persistence with Hibernate, which is considered the "Hibernate Bible" amongst my co-workers.

http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1

Critical Failure