1

我在映射@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)

我使用休眠 3.2.6.ga,休​​眠实体管理器 3.3.1.ga。

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

4

1 回答 1

0

当父实体中的 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;
于 2012-10-10T12:38:21.740 回答