嗨下面是我的實體,它們之間的關聯多對一外鍵必須有相同數量爲多對一映射被引用的主鍵列
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」應該進入學生表
的「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)
我試圖解決這個問題,但它顯示同樣的錯誤 請建議我如何解決這個
這是你的實體的完整代碼嗎?像@ @ OneToMany或@ ManyToOne這樣的映射註釋在哪裏?從邏輯上講,學生可以有單一的學生細節映射,反之亦然,所以映射應該是'@ oneToOne'嗎? – Chaitanya 2014-11-04 06:49:52
請找到更新後的帖子(添加manytoone註釋),我錯過了添加它在 – user3824049 2014-11-04 06:52:50
後添加的答案解決您的問題,請檢查。 – Chaitanya 2014-11-04 06:57:47