2014-11-04 43 views
0

嗨下面是我的實體,它們之間的關聯多對一外鍵必須有相同數量爲多對一映射被引用的主鍵列

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) 

我試圖解決這個問題,但它顯示同樣的錯誤 請建議我如何解決這個

+0

這是你的實體的完整代碼嗎?像@ @ OneToMany或@ ManyToOne這樣的映射註釋在哪裏?從邏輯上講,學生可以有單一的學生細節映射,反之亦然,所以映射應該是'@ oneToOne'嗎? – Chaitanya 2014-11-04 06:49:52

+0

請找到更新後的帖子(添加manytoone註釋),我錯過了添加它在 – user3824049 2014-11-04 06:52:50

+0

後添加的答案解決您的問題,請檢查。 – Chaitanya 2014-11-04 06:57:47

回答

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實體表的外鍵被稱爲userrole指的是StudentDetails實體表中名爲VALUE的列。

然後在接下來的行中,您再次告訴休眠使用相同的柱面名稱 - userrole作爲FKey在StudentDetails中的列DESCRIPTION,所以此行覆蓋了前一個。

因此,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]) 

其他信息:

您試圖聲明一個複合的ID爲實體StudentDetails,所以這個實體應該implelemt Serializable接口,這是強制性的。 現在這個複合Id類應該覆蓋equals()hashcode()方法。

只是一個建議,嘗試遵循您的實體和字段的Java命名約定。

+0

什麼是userrole_value和userrole_desc列,我們的表 – user3824049 2014-11-04 07:18:11

+0

@ user3824049中沒有這樣的列,那麼請更新您的問題並顯示您的列的外觀。你需要告訴hibernate Student表中的外鍵是什麼。根據你的問題,你正在創建一個名爲'userrole'的列,並且告訴hibernate將它映射到2列student_details表,這是不正確的。 – Chaitanya 2014-11-04 07:20:48

+0

感謝您的意見,請查看添加的表格細節和我的要求 – user3824049 2014-11-04 07:27:10

相關問題