下面是我的实体,他们之间有很多关联
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 );
}
下面的是我的2表中的列
student:
UserID
userrole
student_details:
VALUE
DESCRIPTION
“student_details”中的“value”应该输入学生表的“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)
我试图解决此问题,但它显示相同的错误 请建议我如何解决这个问题
答案 0 :(得分:1)
要解决此问题,请更改以下代码:
@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在名为Student
的{{1}}实体表中创建一个外键,该表引用userrole
实体表中名为VALUE
的列。
然后在下一行中,您再次告诉hibernate使用相同的列名称StudentDetails
作为userrole
中DESCRIPTION
列的FKey,因此该行会覆盖前一行。< / p>
所以hibernate看到你试图在映射到StudentDetails
实体表的Student
实体表中将单个列作为外键。但StudentDetails
表的复合键由2列组成,因此hibernate会抛出异常。
StudentDetails
其他信息:
您正在尝试为实体org.hibernate.MappingException: Foreign key (FK6D56043A4415BDB5:student [userrole])) must have same number of columns as the referenced primary key (student_details [VALUE,DESCRIPTION])
声明一个复合ID,因此该实体应该实现StudentDetails
接口,这是必需的。
现在,此复合Id类应覆盖Serializable
和equals()
方法。
只是一个建议,尝试遵循您的实体和字段的Java命名约定。