Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Inheritance..

 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Richardson
author
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic