2

我有以下问题:

我有一个带有复合主键的基类(用作超类)。现在我正在尝试从基类正确设置子类,但这不起作用!有人可以帮我吗?谢谢

这是我的课程:

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;
  ..
}

子类A.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;
  ..
}

子类B.java

@Entity
public class SubClassB extends Base implements Serializeable {

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

  protected String attrB1;
  protected String attrB2;
  ..
}

子类C.java

@Entity
public class SubClassC extends SubClassA implements Serializeable {


  protected String attrC1;
  protected String attrC2;
  ..
}

当我尝试运行该项目时,会出现以下异常:

[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)])

仅供参考:equals 和 hashcode 方法在所有类中都被覆盖。

4

1 回答 1

1

首先,移除 Base 上的 @Inheritance 注解。它没有任何目的。

然后,尝试为 ManyToOne 关联显式定义连接列。默认情况下,EclipseLink 似乎对嵌入式 ID 字段和与 SubClassA 的多对一关联使用相同的列名。有关使用此注释的示例,请参阅http://docs.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html 。

旁注:复合 ID 既低效又难以处理。您真的应该考虑为所有实体使用自动生成的单列 ID。一切都会容易得多。

于 2012-11-01T23:01:50.970 回答