2012-11-01 43 views
2

我有以下問題:嘗試在超類中使用EmbeddedId

我有一個基類(用作超類)和一個複合主鍵。現在我試圖從基類中正確設置子類,但這不起作用!有人能幫助我嗎?由於

這裏是我的課:

BasePK.java

@Embeddable 
public class BasePK implements Serializeable { 
    protected String base1; 
    protected Timestamp base2; 
    .. 
} 

Base.java

@MappedSuperclass 
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) 
public abstract class Base implements Serializeable { 
    @EmbeddedId 
    protected BasePK id; 
    .. 
} 

SubClassA.java

@Entity 
public class SubClassA extends Base implements Serializeable { 

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="attrA") 
    protected List<SubClassB> attrsB; 

    protected String attrA1; 
    protected String attrA2; 
    .. 
} 

SubClassB.java

@Entity 
public class SubClassB extends Base implements Serializeable { 

    @ManyToOne(fetch=FetchType.LAZY) 
    private SubClassA attrA; 

    protected String attrB1; 
    protected String attrB2; 
    .. 
} 

SubClassC.java

@Entity 
public class SubClassC extends SubClassA implements Serializeable { 


    protected String attrC1; 
    protected String attrC2; 
    .. 
} 

當我嘗試運行該項目,下面excetions由:

[EL Info]: 2012-11-01 23:31:18.739--ServerSession(2063330336)--EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345 
[EL Warning]: metadata: 2012-11-01 23:31:19.199--ServerSession(2063330336)--Reverting the lazy setting on the OneToOne or ManyToOne attribute [controlledObject] for the entity class [class logyourlife.entities.ChangeRecord] since weaving was not enabled or did not occur. 
[EL Severe]: 2012-11-01 23:31:19.229--ServerSession(2063330336)--Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.IntegrityException 
Descriptor Exceptions: 
--------------------------------------------------------- 

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DescriptorException 
Exception Description: Multiple writable mappings exist for the field [SUBCLASSB.BASE1]. Only one may be defined as writable, all others must be specified read-only. 
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[attrA] 
Descriptor: RelationalDescriptor(test.entities.SubClassB --> [DatabaseTable(SUBCLASSB)]) 

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DescriptorException 
Exception Description: Multiple writable mappings exist for the field [SUBCLASSB.BASE2]. Only one may be defined as writable, all others must be specified read-only. 
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[attrA] 
Descriptor: RelationalDescriptor(test.entities.SubClassB --> [DatabaseTable(SUBCLASSB)]) 

fyi:平等s和hashcode方法在所有類中被覆蓋。

回答

1

首先,刪除Base上的@Inheritance註釋。它不起任何作用。

然後,嘗試明確定義ManyToOne關聯的連接列。看起來EclipseLink默認使用相同的列名,既用於嵌入式ID字段,也用於與SubClassA的多對一關聯。有關使用此註釋的示例,請參閱http://docs.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html

備註:複合ID既低效又複雜。您應該真的考慮爲所有實體使用自動生成的單列ID。一切都會更容易。

+0

我已經將@JoinColumn註釋添加到SubClassB中,以便base1和base2現在可以工作。感謝您的幫助! –

相關問題