• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mapping subclass primary key with Hibernate

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Petter Black
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Redesigning was a good choice. Especially if these are new tables. Good luck
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic