• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Foreign key must have same number of columns

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please add a compoist key for STUDENT_ADDRESS with properties STUDENT_ID,ADDRESS_ID
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Debian Fedora Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh Rj wrote:Thanks for the link.

Welcome to javaranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic