2015-02-06 55 views
0

我有這個@ElementCollection映射,所以我可能會帶來不唯一ID的遺留表的工作:@ElementCollection非集合場

@Entity @Table(...) 
@Inheritance(...) @DiscriminatorColumn(...) 
class Notification { 
    @Id 
    @Column(name="NOTIFICATION_ID") 
    private BigInteger id; 
} 

@Entity 
@DiscriminatorValue(...) 
class SomeNotification extends Notification { 

    @ElementCollection 
    @CollectionTable(name="LEGACY_TABLE", [email protected](name="NOTIFICATION_ID")) 
    private Set<NotificationInfo> someInformations; 

} 

@Embeddable 
class NotificationInfo { // few columns } 

我真的不能碰LEGACY_TABLE的結構,現在我面對這樣的:

@Entity 
@DiscriminatorValue(...) 
class SpecialNotification extends Notification { 

    // ? This is not a Collection, and it can't be a ManyToOne or OneToOne 
    // since there is no ID declared on NotificationInfo. 
    private NotificationInfo verySpecialInformation; 

} 

我知道這是不是默認支持的,但我很好實現定製,使其與EclipseLink的工作。關鍵是,對於SpecialNotification實例,最多隻能關聯一個NotificationInfo,而不是很多,這就是SomeNotification的情況。

有關我可以在Customizer中開始的地方的任何想法?

謝謝!

回答

1

我不確定這會起作用,但它值得一試。請嘗試@SecondaryTable組合和@AttributeOverride

@Entity 
@SecondaryTable(name="LEGACY_TABLE", 
     [email protected](name="NOTIFICATION_ID")) 
@DiscriminatorValue(...) 
class SpecialNotification extends Notification { 
    ... 
    @Embedded 
    @AttributeOverrides({ 
     @AttributeOverride(name="someField", [email protected](table = "LEGACY_TABLE", name="SOME_FIELD")), 
     @AttributeOverride(name="someOtherField", [email protected](table = "LEGACY_TABLE", name="SOME_OTHER_FIELD")) 
    }) 
    private NotificationInfo verySpecialInformation; 
    ... 
} 

UPDATE

由於@SecondaryTable默認情況下,使內部聯接,這可能是不期望的,它可以與周圍的供應商特定的API工作。

如果您使用休眠(您不這樣做,通過問題標籤來判斷,但不過),可以通過設置optional = true來完成@org.hibernate.annotations.Table

用的EclipseLink,你應該利用@DescriptorCustomizerDescriptorQueryManager#setMultipleTableJoinExpression,你可以找到一個(不是噴滴,但足夠接近)的代碼示例here

+0

我以爲你知道,但是SecondaryTable意味着一個Inner Join。你知道這種關係是可選的。 – 2015-02-06 13:14:27

+0

是的,會使用'inner join'。另外一個選擇是創建一個數據庫視圖,在其中您將「外部連接」可選數據。它甚至可以[支持插入和更新](http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Views),即使觸發器用於實現它。 – 2015-02-06 13:33:03

+0

如果您使用Hibernate,您可以將其標記爲可選 http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#mapping-declaration-join – 2015-02-06 14:02:35