foreign key must have same number of columns as the referenced primary key for manytoone mapping

*爱你&永不变心* 提交于 2020-12-27 04:01:31

问题


Hi below is my entities with manytoone association between them

student.java

@Entity
@Table(name = "student")
public class student{

    @Id
    @Column(name = "UserID")
    private String userid;

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumns({
       @JoinColumn(name = "userrole", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole", referencedColumnName = "DESCRIPTION")

    })
    private studentdetails userrole;

//setters and getters 
//constructor

}

studentdetails.java

@Data
@Entity
@Table(name = "student_details")
public class studentdetails {

    @Id
    @Column(name = "VALUE")
    private String value;

    @Id
    @Column(name = "DESCRIPTION")
    private String description;

   //setters and getters 
   //constructor
}

appmain.java

public static void main()
{

//session configuration

studentdetails  sd = new studentdetails();
sd.setvalue("abc");
sd.setdescription("abcdef");

student student1 = new student();
student.setuserid("1");
student.userrole(sd);

student student2 = new student();
student.setuserid("2");
student.userrole(sd);

session.save(student1 );
session.save(student2 );


}

below are the columns in my 2 table

student: 

UserID
userrole

student_details:

VALUE
DESCRIPTION

the "value" in "student_details" should enter into "userrole" of student table

but when I execute my appmain I am getting below error

org.hibernate.MappingException: Foreign key (FK6D56043A4415BDB5:student [userrole])) must have same number of columns as the referenced primary key (student_details [VALUE,DESCRIPTION])
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1354)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)

I tried to solve this but it showing same error please suggest me how to solve this


回答1:


To fix the issue, change your code like this:

 @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumns({
       @JoinColumn(name = "userrole_value", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole_desc", referencedColumnName = "DESCRIPTION")

    })
    private studentdetails userrole;

Reason for the issue: In mapping:

@JoinColumns({
       @JoinColumn(name = "userrole", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole_desc", referencedColumnName = "DESCRIPTION")

    })

You are telling hibernate to create a foreign key in Student entity table called userrole that refers to the column called VALUE in StudentDetails entity table.

Then in next line you are again telling hibernate to use the same colum name - userrole as FKey for the column DESCRIPTION in StudentDetails, so this line overrides the previous one.

So hibernate sees that you are trying to have a single column as foreign key in Student entity table that maps to StudentDetails entity table. But the StudentDetails table has composite key made of 2 columns so hibernate throws an exception.

org.hibernate.MappingException: Foreign key (FK6D56043A4415BDB5:student [userrole])) must have same number of columns as the referenced primary key (student_details [VALUE,DESCRIPTION])

Additional information:

You are trying to declare a composite Id for the entity StudentDetails, so this entity should implelemt Serializable interface and this is mandatory. Now this composite Id class should override equals() and hashcode() methods.

Just a suggestion, try to follow Java naming conventions for your entities and fields.



来源:https://stackoverflow.com/questions/26728949/foreign-key-must-have-same-number-of-columns-as-the-referenced-primary-key-for-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!