2015-02-06 58 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