2013-02-10 97 views
0

Entity模樣JPA:@Embedded實體如何將自己的類型作爲字段?

@Entity 
public class Market extends MutableEntity { 

    @Column(nullable = false) 
    private String name; 
    @Column 
    private String description; 
    @Embedded 
    private Version marketVersion; ..snipped 

具有@Embedded Version作爲

@Embeddable 
public class Version { 
    private String number; 
    private VersionType versionType; 
    private DateTime publishedOn; 
    private DateTime retiredOn; 
    private Version parentVersion; 

但失敗

08:12:23.481 [main] DEBUG o.h.cfg.annotations.PropertyBinder - Building property number 
08:12:23.481 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Market), mappingColumn=parentVersion, insertable=true, updatable=true, unique=false} 

    java.lang.StackOverflowError 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 
     at org.hibernate.cfg.AbstractPropertyHolder.getExactOverriddenColumn(AbstractPropertyHolder.java:179) 

這究竟是爲什麼以及如何解決這個問題?

回答

5

這是無法完成的。此外,它並沒有什麼太大的意義,以支持這樣的結構,因爲那時沒有遏制地方結束的任何一點:Version包含Version,其中包含Version,其中包含Version

在JPA 2.0規範,這是告訴與以下文字:

不允許在可嵌入的 類之間直接或間接循環包含依賴項。

+0

我明白了,謝謝@Mikko,但是我可以通過'JPA'擁有包含'parentVersion'的'version'的方式是什麼? – daydreamer 2013-02-10 18:27:12

+0

通常在這種情況下,版本是實體,並且具有ManyToOne關係自身(從小孩到父母)。如果需要雙向關係,則另外OneToMany(集合值)向其他方向。 – 2013-02-10 18:34:26

相關問題