aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Inheritance.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Inheritance.." Watch "Hibernate Inheritance.." New topic
Author

Hibernate Inheritance..

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
I'm a little cloudy when it comes to the solutions provided by Hibernate for object inheritence..

In the example where you have a person class, and subclasses such as employee, customer etc...

I understand one method, and the easiest it seems to work with (and most suggested it appears) is for Hibernate to create a Single table containing all fields contained in emplyee & customer..

Doesnt this approach run against everything what we're taught in the early days of database design..

Surely the table would become heavily populate with null values....

Thats my thoughts anyway....


Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Doesnt this approach run against everything what we're taught in the early days of database design..

Absolutely. I think its a poor idea, verging on an anti-pattern. Personally I'd always design the ER model first and derive the object model from this. Where I work we have one schema accessed by about twenty different applications written in a variety of different languages; only three use the Hibernate layer to access the DB. That the database handles its own data integrity (as is right and proper) is paramount, regardless of what the designers of any ORM solution might suggest we do.
[ January 25, 2006: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
For my particular application i'm designing, I really like hibernates concepts in the sense of decoupling from the database specifics.. At the moment i'm using mysql but maybe I will change later. Not having to really code mysql specific sql is a blessing.

It was just that niggling inheritence issue that seems a bit wacky..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate itself has a lot going for it. But as with any technology, it can be misapplied, particularaly when the technology's own documentation contains instructions as to how to misapply it!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Paul, I agree with you here and I've been struggling to get Hibernate to respect data integerity. (This is related to my earlier post) Here is an example: suppose I have an ordered many-to-one association, with the surrogate PKs for both tables, a situation that seems pretty normal to me:

table WHOLE:
WHOLE_ID <PK>
etc...

table PART:
PART_ID <PK>
WHOLE_ID <FK>
POSITION //0,1,2...
etc...

Now I'd like PART columns WHOLE_ID and POSITION to both be NOT NULL and there to be a unique index on the pair of them -- there can only be one part at position 0 for whole #17, for example. But because of the SQL that Hibernate generates neither of these fields can be NOT NULL (because Hibernate first inserts a row with NULL values then updates these two fields ?!). The unique index doesn't work either, becaue if you rearrange tbe position of parts, the update statements Hibernate generates can temporarily have more than one part for a given whole with the same position value. I've tried different variations in the mapping document, but nothing will respect both these database constraints. What can I do? Currently, I'm going with a database design that lets Hibernate operate but I don't like it.


There is no emoticon for what I am feeling!
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Jeff,

Have you tried using <key non-null="true">. For example:

<list name="employees" cascade="all-delete-orphan">
<key column="DEPARTMENT_ID" not-null="true"/>
<index column="DEPT_INDEX" />
<one-to-many class="Employee" />
</list>

See http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-key

Chris


Enterprise Java consulting and training - <a href="http://www.chrisrichardson.net" target="_blank" rel="nofollow">http://www.chrisrichardson.net</a> Author, POJOs in Action - <a href="http://www.manning.com/crichardson" target="_blank" rel="nofollow">http://www.manning.com/crichardson</a> Enterprise POJOs blog - <a href="http://chris-richardson.blog-city.com" target="_blank" rel="nofollow">http://chris-richardson.blog-city.com</a>
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I've tried various combinations, but you're right, that will add a NOT NULL constraint to the FK. Here is mapping that comes close (but no cigar) to a correct, ordered many-to-one mapping:

In MySQL, Part looks like:

Notice that position is nullable If I go into the database later and add a NOT NULL constraint I get into trouble because Hibernate generates this SQL, when you save a whole with two parts:

Hibernate: insert into Whole (name) values (?)
Hibernate: insert into Part (name, whole_id, position) values (?, ?, ?)
Hibernate: insert into Part (name, whole_id, position) values (?, ?, ?)
Hibernate: update Part set whole_id=?, position=? where part_id=?
Hibernate: update Part set whole_id=?, position=? where part_id=?

That unfortunate insert/then update into Part means that position is briefly NULL

Similarly, there is no unqiueness constraint on the pair of columns whole_id + position, and again you can't add it because the SQL that Hibernate generates when you reorder parts in a whole may briefly have two parts associated with the same whole at the same position

So again my problem is that the SQL Hibernate generates doesn't take my reasonable constraints into account. Is there a solution that allows me to have a proper database design?
[ January 26, 2006: Message edited by: Jeff Albrectson ]
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Jeff,

Which version of Hibernate are you using?
I am using Hibernate 3.1.
My example worked with a NOT NULL constraint on the position column.
I also looked at the debug output, which showed the insert specifying a value for that column. The UPDATE statement seemed to be gratuitous.

Chris
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
By Jove, you're right about setting position field to NOT NULL! I must have been trying to make too many changes at once. To get the NOT NULL constraint, I added this to my Part mapping:

<property name="position" insert="false" update="false" not-null="true"/>

I'm still doubtful that I can get a unique index on WHOLE_ID + POSITION, but when I try to create that with a properties element, I'm getting a serialization error ?! I'll have to try again tomorrow, when I have some more time.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I don't know how I got a serialization error before, but here is my best effort: it generates the NOT NULL constraints you would want and the unique index on WHOLE_ID + POSITION in the PART table:

Maybe it's just too much to expect Hibernate to handle that uniqueness constraint. I asked myself, "How would you rearrange parts under that constraint, in an intelligent manner?" and it took a bit a head scratching to come up with a way.
Eric Samson
Greenhorn

Joined: Sep 01, 2004
Posts: 18
If you are interested in comparing various inheritance mapping strategies you might find the following article appealing:

http://www.xcalia.com/xdn/resources/articles/InheritanceStrategy/PickingTheRightInheritanceStrategy.jsp

The source code is also available, so that you can test it yourself.

Best Regards, Eric.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Inheritance..
 
Similar Threads
Hibernate: one class many tables
many-to-many mapping in the hibernate mapping file ?
How does Hibernate handle a nullable foreign key in a query?
Hibernate : Could not execute query -- Exception
Composite Key Mapping with OneToMany does not work