【问题标题】:Hibernate polymorphic collection mapping with annotation带有注释的 Hibernate 多态集合映射
【发布时间】:2014-09-07 06:50:00
【问题描述】:

我为我的客户准备了一个抽象类:

@Entity
public class AbstractClientEntity extends AbstractPersonneEntity implements Demarchable {

@Column(name="ID_REFERENT")
private AbstractEmployeEntity referent;
...

具体类如下所示:

@Entity
@DiscriminatorValue(TypePersonne.Values.INVITE)
public class InviteEntity extends AbstractClientEntity implements Invitable {
...

@Entity
@DiscriminatorValue(TypePersonne.Values.AUTRE)
public class AutreClientEntity extends AbstractClientEntity {

我为拥有一组 AbstractClientEntity 的员工提供了另一个抽象类:

@Entity
public class AbstractEmployeEntity extends AbstractPersonneEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_REFERENT", nullable = false)
private MonitriceEntity monitrice = new MonitriceEntity();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "referent")
private Set<AbstractClientEntity> listeClients = new HashSet<AbstractClientEntity>();
...

问题是当我尝试运行我的网络应用程序时,抛出了这个异常:

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory ch.tupperware.tuppergestion.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/config/tupper-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: ch.tupperware.tuppergestion.entity.AbstractEmployeEntity, at table: PERSONNE, for columns: [org.hibernate.mapping.Column(ID_REFERENT)]
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:825)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:767)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)

我不知道如何解决这个问题...

感谢您的帮助

【问题讨论】:

    标签: java hibernate collections mapping polymorphism


    【解决方案1】:

    原因很清楚:

    Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: ch.tupperware.tuppergestion.entity.AbstractEmployeEntity, at table: PERSONNE, for columns: [org.hibernate.mapping.Column(ID_REFERENT)]
    

    您的班级 ch.tupperware.tuppergestion.entity.AbstractEmployeEntity 没有在您的表 PERSONNE 中找到列 ID_REFERENT

    你是如何映射你的AbstractEmployeEntity 类的。如果是抽象类,为什么不用@MappedSuperclass映射呢?

    【讨论】:

    • 感谢您的快速回答。我发誓 ID_REFERENT 字段在我的 PERSONNE 表中。我确实用@MappedSuperclass 映射了我的抽象类,因为我希望能够在我的AbstractEmployeEntity 中拥有一个AbstractClientEntity 的集合。当我尝试使用 @MappedSuperclass 注释时,异常 org.hibernate.AnnotationException: Use of @OneToMany 或 @ManyToMany 针对未映射的类:ch.tupperware.tuppergestion.entity.AbstractEmployeEntity.listeClients[ch.tupperware.tuppergestion.entity.AbstractClientEntity ] 被抛出...
    • 我建议你只注释@MappedSuperclass only 抽象类而不是具体类。
    • 我试图用 @MappedSuperClass 注释我的所有抽象类,但现在我有与上述类似的异常,但在一个具体类上:嵌套异常是 org.hibernate.AnnotationException:使用 @OneToMany 或 @针对未映射类的多对多:ch.tupperware.tuppergestion.entity.AmbassadriceEntity.listeClients[ch.tupperware.tuppergestion.entity.AbstractClientEntity]
    【解决方案2】:

    最后,我会回答我自己的问题,希望它对某人有所帮助......

    经过多次尝试,我采用了一个解决方案,为我必须在我的 AbstractEmployeEntity 中管理的每个具体类创建一个列表:

    @Entity
    public class AbstractEmployeEntity extends AbstractPersonneEntity {
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "referent")
    private Set<HotesseEntity> listeHotesse = new HashSet<HotesseEntity>();
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "referent")
    private Set<InviteEntity> listeInvites = new HashSet<InviteEntity>();
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "referent")
    private Set<AutreClientEntity> listeAutreClients = new HashSet<AutreClientEntity>();
    

    希望这会对某人有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多