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).
Columns: A and B. Primary Key: A
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?
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.
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!