2010-08-11 57 views
0

我使用:NetBeans IDE 6.7.1,GlassFish v2.1,Oracle 10g XE,JAVA 6 SE, JAVA 5 EE。來自實體類[...]的帶註釋元素的@JoinColumns是不完整的

我有一個@ManyToMany註釋的問題:在GlassFish 2.1.1的輸出

@ManyToMany(fetch=FetchType.EAGER) 
@JoinTable(name="CUST_RENT_MOVIE", joinColumns={@JoinColumn(name="CUST_ID")}, inverseJoinColumns={@JoinColumn(name="TITLE")}) 
private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>(); 

部分

Exception Description: The @JoinColumns on the annotated element [private java.util.Collection vc.domain.Customer.rents] from the entity class [class vc.domain.Customer] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referenceColumnName elements must be specified in each such @JoinColumn. 
javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.1 (Build b31g-fcs (10/19/2009))): oracle.toplink.essentials.exceptions.EntityManagerSetupException 
Exception Description: predeploy for PersistenceUnit [vc_pu] failed. 

部分腳本創建數據庫:

CREATE table customer 
(
cust_id NUMBER(5),  
CONSTRAINT cust_pk PRIMARY KEY (cust_id), 
... 
) 

CREATE TABLE movie 
(
title VARCHAR2(50) PRIMARY KEY, 
... 
) 

CREATE TABLE cust_rent_movie 
(
title VARCHAR2(50), 
cust_id NUMBER(5), 
rent_date DATE DEFAULT current_date NOT NULL, 
return_date DATE, 
CONSTRAINT cust_rent_movie_pk PRIMARY KEY (title, cust_id, rent_date), 
CONSTRAINT CustRentMovie_movie_fk FOREIGN KEY (title) REFERENCES movie ON DELETE CASCADE, 
CONSTRAINT CustRentMovie_cust_fk FOREIGN KEY (cust_id) REFERENCES customer ON DELETE CASCADE 
) 

客戶類別代碼

@Entity 
@Table(name = "customer") 
@SequenceGenerator(name="seq", sequenceName="cust_id_seq", allocationSize=1) 
public class Customer implements Serializable 
{ 

    @Id 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") 
    @Column(name="CUST_ID") 
    private int id; 


    @ManyToMany(fetch=FetchType.EAGER) 
    @JoinTable(name="CUST_RENT_MOVIE", joinColumns={@JoinColumn(name="CUST_ID")}, inverseJoinColumns={@JoinColumn(name="TITLE")}) 
    private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>(); 


    public Collection<CustRentMovie> getRents() 
    { 
     return rents; 
    } 

    public void setRents(Collection<CustRentMovie> rents) 
    { 
     this.rents = rents; 
    } 
... 
} 

錯我的收藏把類型類CustRentMovie而不是電影,所以我改變了

private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>(); 

private Collection<Movie> movies = new ArrayList<Movie>(); 
+0

只是可以肯定,你真的要映射的許多'Customer'和'CustRentMovie'之間的多對多關聯(或者'Customer'和'Movie'之間的多對多關聯)? – 2010-08-11 21:35:44

+0

我想映射客戶和電影之間的多對多關聯。 CustRentMovie是連接表。該協會在紅色的日子和返回的日子中具有屬性。由於客戶可能會租用同一部電影,因此我將red_date添加到主鍵。 – IVANNHS 2010-08-12 04:54:09

+0

這就是我的想法。 – 2010-08-13 04:05:57

回答

1

CustomerRentMovie的PK是由三列(標題,cust_id,rent_date)。 Toplink表示您需要在@JoinColumn註釋中指定所有三個連接列(目前您只指定了cust_id)。

將註釋更改爲這樣的應該會讓你過去這個錯誤。

@JoinTable(name = "CUST_RENT_MOVIE", joinColumns = { 
    @JoinColumn(name = "CUST_ID"), 
    @JoinColumn(name = "TITLE"), 
    @JoinColumn(name = "RENT_DATE") }, inverseJoinColumns = { @JoinColumn(name = "TITLE") }) 
private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>(); 

這就是說,帕斯卡的問題是有效的 - 它看起來像你預期有客戶向電影,而不是客戶向CUST_RENT_MOVIE的關係。

0

錯我的收藏把類型類CustRentMovie而不是電影,所以我改變了

private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>(); 

private Collection<Movie> movies = new ArrayList<Movie>(); 
+0

請更新您的問題以反映這一點。 TIA。 – 2010-08-13 04:05:08

+0

您的意思是將此答案設置爲我接受的答案?或者我必須做其他事情。 – IVANNHS 2010-08-13 05:28:20

+0

不,這不是我的意思 - 我認爲邁克的回答是一個更好的候選人:)我的意思是使用最初問題底部的[編輯]鏈接來編輯它的內容(難道你沒有那個' [鏈接?]。 – 2010-08-13 16:59:34

相關問題