gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:20:47 26 4
gpt4 key购买 nike

你好,下面是我的实体,它们之间有 manytoone 关联

学生.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
}

应用程序主程序

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 表中的列

student: 

UserID
userrole

student_details:

VALUE
DESCRIPTION

“student_details”中的“value”应该进入student表的“userrole”

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

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)

我试图解决这个问题,但它显示同样的错误请建议我如何解决这个问题

最佳答案

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

 @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")

})

你告诉 hibernate 在名为 userroleStudent 实体表中创建一个外键,它引用 中名为 VALUE 的列StudentDetails实体表。

然后在下一行中,您再次告诉 Hibernate 使用相同的列名 - userrole 作为 StudentDetails 中列 DESCRIPTION 的 FKey,所以这一行覆盖了前一行。

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

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 接口(interface),这是强制性的。现在这个复合 Id 类应该覆盖 equals()hashcode() 方法。

只是一个建议,尽量遵循 Java 命名约定为您的实体和字段。

关于hibernate - 外键必须与多对一映射的引用主键具有相同的列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26728949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com