2011-04-02 108 views
1

我有兩個表。複合主鍵選擇 - Hibernate的哪些功能不適用於EclipseLink。爲什麼?

CREATE TABLE profile_table 
( 
    profile_id NUMBER(10,0) PRIMARY KEY, 
    creator_manager_id NUMBER(10,0), 
    lastupdate DATE, 
    description VARCHAR2(255 BYTE), 
    profile_name VARCHAR2(255 BYTE), 
    creatorname VARCHAR2(255 BYTE) 
); 

CREATE TABLE profile_basket_table 
( 
    profile_id NUMBER(10,0), 
    from_id NUMBER(10,0), 
    action NUMBER(10,0), 
    constraint pbt_pk_constraint PRIMARY KEY(profile_id, from_id), 
    constraint pbt_fk_constraint FOREIGN KEY(profile_id) REFERENCES profile_table(profile_id) 
); 

我有1個配置文件和* ProfileItem。

1配置文件--- * ProfileItems(一個配置文件可以有許多ProfileItems)。

注意:1.它是一個從profile到profileItem的一對多關係。 2.它是一種單向關係。

配置文件的PK是profile_id。

的PK ProfileItem是一個複合主鍵(PROFILE_ID,from_id)

首先,我想我喜歡抱怨的EclipseLink不工作,如Hibernate:

1日問題與EclipseLink的: 只是becasue我有一個名爲ProfileItem的子實體中的複合主鍵,它迫使我使用JoinColumns。我不能使用@JoinColumns作爲其他列from_id不是任何其他表的外鍵。

@Entity 
@Table(name="profile_table") 
class Profile { 

    @Id 
    @Column(name="profile_id") 
    private Integer profileId ; 
    ... 

    /** 
    * WORKS WITH HIBERNATE AS PERSISTENCE PROVIDER, BUT FAILS WITH ECLIPSELINK 
    */ 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinColumn(name= "profile_id", referencedColumnName="profile_id") 
    private Set<XVMUpdateProfileItem> profileItems = null; 

    ... 
} 

以上代碼拋出下面的錯誤與EclipseLink的: 我最初使用與Hibernate的完美工作如下相同註釋

The @JoinColumns on the annotated element [field profileItems] from the entity class [class arun.ucerelay.datastructures.XVMUpdateProfile] 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 referencedColumnName elements must be specified in each such @JoinColumn. 

一些如何與幾個排列@JoinColumn註解的組合,我管理讓這個工作負載配置文件和相關的配置文件項目。

/** 
* WORKS WITH ECLIPSELINK AS PERSISTENCE PROVIDER 
*/ 

@OneToMany(cascade = CascadeType.ALL) 
@JoinColumns({ 
    @JoinColumn(name= "profile_id", referencedColumnName="profile_id" , insertable = true, updatable = true), 
    @JoinColumn(name= "profile_id", referencedColumnName="profile_id" , nullable = false)  
}) 
private Set<XVMUpdateProfileItem> profileItems = null; 

這沒有意義再次聲明@JoinColumn ...但奇怪的是它與eclipselink工作。這是一個錯誤。不是嗎?

任何人都可以告訴我,如果我失去了什麼?有沒有其他正確的方式來實現單向關係,請告訴我。

這是一個關於@JoinColumns的bug嗎?我應該登錄嗎?

這就是所謂的「便攜性」從一個持久性提供者到另一個??????? 如果它不解決可移植性,那麼使用JPA規範有什麼用處。

保存父級和級聯子項的第二個問題與休眠一起使用,並且不適用於eclipselink。我把它作爲一個單獨的問題發佈,以保持簡潔和精確。

謝謝。 Arun Kandregula

+0

作爲參考,DataNucleus將(http://www.datanucleus.org)適用於該元數據細;只有一個@JoinColumn,因爲這是識別ProfileItem中鏈接回配置文件(具有單個PK字段)的FK。 – DataNucleus 2011-04-02 14:38:49

回答

2

這似乎是註解處理中的一個錯誤。如果它仍然出現在最新版本上,請在EclipseLink上記錄此錯誤。

請注意@JoinColumn通常用於@ManyToOne或@OneToOne,而不是@OneToMany。通常@OneToMany你應該有一個反@ManyToOne並使用mappedBy。在@OneToMany上使用@JoinColumn是@OneToMany的一種特殊類型,它維護目標表的外鍵本身。由於profile_id是主鍵的一部分,爲了正確,您應該返回@ManyToOne,然後可以移除profileId並將@Id添加到@ManyToOne。

+0

已經有一個bug在2.3版本中似乎已經修復。 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=336122)。另外,在這裏我使用@OneToMany上的@JoinColumn,因爲我打算實現單向關係。所以我不想添加@ManyToOne。你能告訴我如何刪除profileId,只需將@Id添加到@ManyToOne?我不明白你最後的聲明。 – 2011-04-06 18:30:59

0

我繼承了這樣的代碼(和工作與休眠罰款):

@OneToMany @JoinColumn(name = 「prod_no」,referencedColumnName = 「prod_no」) 私人珍藏物品;

joinColumn用於一對多)

+0

Hibernate工作正常。問題在於舊版本的eclipselink。 – 2011-04-15 06:04:37

相關問題