aspose file tools*
The moose likes Object Relational Mapping and the fly likes Foreign key must have same number of columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Foreign key must have same number of columns" Watch "Foreign key must have same number of columns" New topic
Author

Foreign key must have same number of columns

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

i'm doing many to many in hibernate for a relation of Student and Address

1 student will have permanent address,temporary address like wise any number of student can stay in one address

student.hbm.xml


address.hbm.xml


Student.java


Address.java

Exception
Exception in thread "main" org.hibernate.MappingException: Foreign key (FK250691B08CBB085E:STUDENT_ADDRESS [ADDRESS_ID])) must have same number of columns as the referenced primary key (STUDENT_ADDRESS [STUDENT_ID,ADDRESS_ID])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1263)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at com.demo.Demo.main(Demo.java:26)


Rajesh Rj
Greenhorn

Joined: Dec 30, 2011
Posts: 4
Please add a compoist key for STUDENT_ADDRESS with properties STUDENT_ID,ADDRESS_ID
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Rajesh Rj wrote:Please add a compoist key for STUDENT_ADDRESS with properties STUDENT_ID,ADDRESS_ID


I have composite key for my STUDENT_ADDRESS table.
Rajesh Rj
Greenhorn

Joined: Dec 30, 2011
Posts: 4
The address.hbm.xml , you posted . the table is STUDENT_ADDRESS or is it ADDRESS.

<hibernate-mapping>
<class name="com.home.address.Address" table="STUDENT_ADDRESS">
<id name="addressID" column="ADDRESS_ID" type="java.lang.Integer">
<generator class="increment" />
</id>
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Rajesh Rj wrote:The address.hbm.xml , you posted . the table is STUDENT_ADDRESS or is it ADDRESS.

<hibernate-mapping>
<class name="com.home.address.Address" table="STUDENT_ADDRESS">
<id name="addressID" column="ADDRESS_ID" type="java.lang.Integer">
<generator class="increment" />
</id>


it is STUDENT_ADDRESS
Rajesh Rj
Greenhorn

Joined: Dec 30, 2011
Posts: 4
When a table has composite key, then in the mapping also Composite id should be added.

Following code snippet, may work.


<class name="com.home.address.Address" table="STUDENT_ADDRESS">
<composite-id>
<key-property name="addressID" column="ADDRESS_ID" type="java.lang.Integer" />
<key-property name="student_id" column="STUDENT_ID" type="java.lang.Integer" />
</composite-id>
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Rajesh Rj wrote:When a table has composite key, then in the mapping also Composite id should be added.


I like you to look at this .
Rajesh Rj
Greenhorn

Joined: Dec 30, 2011
Posts: 4
Thanks for the link.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Rajesh Rj wrote:Thanks for the link.

Welcome to javaranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Foreign key must have same number of columns