外键必须具有与多对一映射所引用的主键相同的列数

外键必须具有与多对一映射所引用的主键相同的列数

本文介绍了外键必须具有与多对一映射所引用的主键相同的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,下面是我的实体,它们之间具有多音素关联

Hi below is my entities with manytoone association between them

student.java

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

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

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 );


}

下面是我的2个表格中的列

below are the columns in my 2 table

student:

UserID
userrole

student_details:

VALUE
DESCRIPTION

"student_details"中的值"应输入到学生表的用户"中

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

但是当我执行我的appmain时,我遇到了错误

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 errorplease suggest me how to solve this

推荐答案

要解决此问题,请像这样更改代码:

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;

问题的原因:在映射中:

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

    })

您要告诉休眠在Student实体表中创建外键,该表称为userrole,该外键引用StudentDetails实体表中称为VALUE的列.

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.

然后在下一行中,您再次告诉hibernate为StudentDetails中的列DESCRIPTION使用与FKey相同的列名-userrole,因此此行将覆盖上一行.

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.

因此hibernate看到您正在尝试在Student实体表中将单个列作为外键映射到StudentDetails实体表.但是StudentDetails表具有由2列组成的复合键,因此休眠会抛出异常.

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])

其他信息:

您正试图为实体StudentDetails声明一个复合ID,因此该实体应实现Serializable接口,这是强制性的.现在,此复合Id类应覆盖equals()hashcode()方法.

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.

只是一个建议,请尝试为您的实体和字段遵循Java命名约定.

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

这篇关于外键必须具有与多对一映射所引用的主键相同的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:24