@OneToOne映射不适用于@Inheritance

时间:2012-10-10 10:54:20

标签: hibernate jpa jboss persistence

我在映射@OneToOne并在jboss下部署​​时遇到问题。

我有类的层次结构:

@Entity
@Table(name = "SEED")
@Inheritance
@DiscriminatorColumn(name = "ENTITY_TYPE")
@ForceDiscriminator
public class Parent implements Serializable
{
   @Id
   @SequenceGenerator(name = "SEED_SEQ", sequenceName = "SEED_SEQ")
   @GeneratedValue(generator = "SEED_SEQ")
   @Column(name = "AA_ID")
   protected Long aaId;

   @Column(name = "SECRET_SEED")
   protected String seed;

   @Column(name = "ENTITY_ID") 
   protected String entityId; 

   //getters and setters
}

@Entity
@DiscriminatorValue(value = "c1")
public class Child1 extends Parent
{
}

@Entity
@DiscriminatorValue(value = "c2")
public class Child2 extends Parent
{
}

我试图以这种方式映射子类:

@Entity
@Table(name = "CLINIC")
public class Clinic implements Serializable
{
   @Id
   @GeneratedValue(generator = "SEQ_CLINIC_RECID")
   @SequenceGenerator(name = "SEQ_CLINIC_RECID", sequenceName = "SEQ_CLINIC_RECID")
   @Column(name = "ID")
   private Long id;

   @Column(name = "CODE")
   private String code;

   @OneToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "CODE", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
   private Child1 child1;

   //getters and setters
}

当我在jboss-4.2.3.GA上部署它时,我有以下异常:

org.hibernate.AnnotationException: referencedColumnNames(ENTITY_ID) of setup.entities.Clinic.child1 referencing setup.entities.Child1 not mapped to a single property
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:16
5)
        at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:63)
        at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.
java:286)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
        at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
        at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java
:154)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:407)
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)

我使用hibernate 3.2.6.ga,hibernate-entitymanager 3.3.1.ga。

为什么会出现此错误?我的映射是否正确?

1 个答案:

答案 0 :(得分:0)

当parent实体中的id列为“AA_ID”时,您的referencedColumnName引用“ENTITY_ID”。我认为将其更改为“AA_ID”可以解决您的问题。从Hibernate给出的例外情况来看,它似乎是在引用AA_ID的连接列中默认,然后也使用您定义的那个。然后异常表示映射到单个属性。

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CODE", referencedColumnName = "AA_ID", insertable = false,     updatable = false)
private Child1 child1;