OneToMany - org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性

时间:2013-06-15 16:33:28

标签: java hibernate-4.x hibernate-tools openxava

我正在尝试使用OpenXava中捆绑的hibernate-tools.jar从我的域类生成数据库模式。

不幸的是,两个带有 OneToMany 关联的类正在给我带来麻烦。

这是第一个:

@Entity
public class Deceased extends ObjectWithId {

    //stuff

    @OneToMany(mappedBy = "deceased", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @Access(AccessType.PROPERTY)
    public Set<DeceasedTransferDossier> getDeceasedTransferDossier() {
        return deceasedTransferDossier;
    }

    public void setDeceasedTransferDossier(
            Set<DeceasedTransferDossier> transferDossiers) {
        this.deceasedTransferDossier = transferDossiers;
    }

    //other stuff
}

这是另一个:

@Entity
public class DeceasedTransferDossier extends DeceasedDossier {

    //stuff

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deceased_fk")
    @Override
    public Deceased getDeceased() {
        return deceased;
    }

    @Override
    public void setDeceased(Deceased deceased) {
        this.deceased = deceased;
    }

    //other stuff
}

这是引发的异常:

[hibernatetool] An exception occurred while running exporter #2:hbm2ddl (Generates database schema)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.infoone.siglo.entity.DeceasedTransferDossier.deceased in com.infoone.siglo.entity.Deceased.deceasedTransferDossier
      [ant] Exiting C:\Users\AlejandroEduardo\Documents\workspace-sts-3.1.0.RELEASE-2\OpenXava\build.xml.

BUILD FAILED
C:\Users\AlejandroEduardo\Documents\workspace-sts-3.1.0.RELEASE-2\SigloXava\build.xml:46: The following error occurred while executing this line:
C:\Users\AlejandroEduardo\Documents\workspace-sts-3.1.0.RELEASE-2\OpenXava\build.xml:1016: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.infoone.siglo.entity.DeceasedTransferDossier.deceased in com.infoone.siglo.entity.Deceased.deceasedTransferDossier
    at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:444)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:392)
    at org.apache.tools.ant.Target.performTasks(Target.java:413)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
Caused by: C:\Users\AlejandroEduardo\Documents\workspace-sts-3.1.0.RELEASE-2\OpenXava\build.xml:1016: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.infoone.siglo.entity.DeceasedTransferDossier.deceased in com.infoone.siglo.entity.Deceased.deceasedTransferDossier
    at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:226)
    at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:189)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:392)
    at org.apache.tools.ant.Target.performTasks(Target.java:413)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:442)
    ... 16 more
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.infoone.siglo.entity.DeceasedTransferDossier.deceased in com.infoone.siglo.entity.Deceased.deceasedTransferDossier
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:666)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:626)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1586)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1359)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1300)
    at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
    at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:302)
    at org.hibernate.tool.ant.Hbm2DDLExporterTask.createExporter(Hbm2DDLExporterTask.java:51)
    at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39)
    at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
    ... 29 more

让我指出一些可能的伤害来源:

  1. DeceasedTransferDossier 扩展了 DeceasedDossier ,它已经有一个非抽象的 getDeceased()访问者;
  2. 所有实体都有一个共同的父类,名为 Identifiable

    @MappedSuperclass
    public class Identifiable {
    
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
        @Id @GeneratedValue(generator="system-uuid") @Hidden 
        @GenericGenerator(name="system-uuid", strategy = "uuid")
        @Column(length=32)
        public String getId() {
            return id;
        }
    }
    
  3. 让我指出这样的结构已经在Spring-data + Hibernate容器中运行,其中DB模式被成功创建(Spring-data 3.1.2 + Hibernate 4.1.6) OpenXava捆绑了旧版本的Hibernate和Hibernate Tools,因此我尝试手动调整它,将其更新为:

    • Hibernate Tools 4.0.0.CR1(我可以在Maven Repository找到的最新版本);
    • Hibernate 4.0.0.Final(Hibernate Tools 4.0.0.CR1所依赖的版本);
    • 并更新所有必要的依赖级别。

    我希望这样的更新可以解决我的问题,但不幸的是,org.hibernate.AnnotationException保持不变。

1 个答案:

答案 0 :(得分:2)

问题在于您正在扩展DeceasedDossier并且实质上正在改变(或定义?)后代类(DeceasedTransferDossier)和Deceased之间的关系。

我们不知道祖先(DeceasedDossier)的样子,但它可能是一些东西。要么你没有注释这个类,所以当持久性提供者试图“弄清楚”死者对DeceasedTransferDossier的意义时,你会遇到问题。或者,您正在错误地注释它。我想也可能有另一个问题,但我们还需要看看DeceasedDossier。

所以,解决方案并不知道DeceasedDossier发生了什么:

  1. 如果您没有注释DecreasedDossier类,那么在DeceasedTransferDossier上告诉您正在使用PROPERTY访问权限的提供者。

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deceased_fk")
    @Override
    @Access(AccessType.PROPERTY)
    public Deceased getDeceased() {
        return deceased;
    }
    
  2. 如果你正在为已故的档案注释,那么我们需要看到问题来解决它。但是,我打赌你只需在DeceasedTransferDossier的已故吸气器上添加@Access注释,在这种情况下你会暴露根问题(当提供者试图“弄清楚”DeceasedDossier发生了什么时。

  3. 最后,我不得不说这似乎是一种奇怪的关系。我想象DeceasedDossier已经死亡状态(一个实例变量)因为你提到了“非抽象的吸气剂”,但你也在后代中定义了这种关系。既然你的问题不是关于这个问题,我就不会“批评”,但也许会回顾一下你为什么要将它分开。