wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Mapping subclass primary key with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping subclass primary key with Hibernate" Watch "Mapping subclass primary key with Hibernate" New topic
Author

Mapping subclass primary key with Hibernate

Petter Black
Greenhorn

Joined: Aug 12, 2012
Posts: 2
Hi everybody, I searched all over the web and I can't seem to find any answer to my problem.

I'll explain. Suppose I have to tables on MySQL, Person and Student (this example is in order to explain my specific problem but my actual classes aren't these).

Table Person:

Columns: A and B. Primary Key: A

Table Student:

Columns: A, C and D. Primary Key: A and C (Composite Key).

So, column A in Student is a foreign key from table Person. And yes, I need C to be part of the primary key, so I can have in my table Student the same person in several rows but with a different data on C column.

As with OOP this would be Inheritance, my design would consist on two classes, one called Person and another called Student, the last one extends from Person.

Now on my Java Application I'm using Hibernate to map this classes. I'm using table per subclass with hbm.xml files for each class.

The Person.hbm.xml would be



and now is my question, how I write Student.hbm.xml, if I need to specify that my foreign key is column A but besides the primary key is composed by A and C?

I tried:



But this doesn't work, it gives me an error like:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKBB07D2C7DB44EAC6:student[A,C])) must have same number of columns as the referenced primary key (person [A])

So I don't know how to put on the xml file that I have a composed primary key that consist on the foreign key and a column of the subclass.

Please, any help is appreciated, and please forgive my bad english. Thanks in advance.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

It is really hard to visualize anything with A's B's and C's. Giving more realistic examples or real code will generally get more responses.

Sounds like you need composite keys. I will post some snippets from Manning Java Persistence with Hibernate, hopefully it will help you out.



[How To Ask Questions][Read before you PM me]
Petter Black
Greenhorn

Joined: Aug 12, 2012
Posts: 2
Thanks Bill, but I resolved my problem, and was not only about composite keys. What I wanted to do is that a subclass would have an additional field as a primary key regarding the superclass, but after I rethought the problem, I guess Hibernate was right about don't allow me to do it. I think if I'd do this way, I would be against the concept of inheritance. So I better redisigned my database. Thanks a lot anyway, by the way that example you gave me is interesting, I'm a noob in Hibernate so maybe it will be helpful to my project anyway. See ya!
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Redesigning was a good choice. Especially if these are new tables. Good luck
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping subclass primary key with Hibernate
 
Similar Threads
polymormsim related demos
Foreig key must have same number of columns as the referenced primary key
org.hibernate.NonUniqueObjectException
Problem with joining of two tables with Hibernate
Parent class id value instead of child's id used incorrectly for querying Set assoc.