gpt4 book ai didi

java - OneToMany - org.hibernate.AnnotationException : mappedBy reference an unknown target entity property

转载 作者:行者123 更新时间:2023-12-02 06:57:28 24 4
gpt4 key购买 nike

我正在尝试使用 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. 所有实体都有一个共同的父类,名为可识别:

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

让我指出,这样的结构已经在 Spring-data + Hibernate 容器中工作,其中数据库模式已成功创建(Spring-data 3.1.2 + Hibernate 4.1.6)OpenXava 捆绑了旧版本的 Hibernate 和 Hibernate Tools,因此我尝试手动调整它,将其更新为:

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

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

最佳答案

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

我们不知道祖先(DeceasedDossier)长什么样,但可能有一些东西。要么您没有注释此类,所以当持久性提供者试图“弄清楚”死者对DeceasedTransferDossier 意味着什么时,您就会遇到问题。或者,您注释不正确。我想可能还存在另一个问题,但我们再次需要查看已故文件。

因此,解决方案无需真正了解DeceasedDossier 发生了什么:

  1. 如果您没有注释 DecreasedDossier 类,则只需告诉提供商您在 DeceasedTransferDossier 上对死者使用 PROPERTY 访问权限即可。

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

最后,我不得不说,这似乎是一种奇怪的关系。我想 DeceasedDossier 具有已故状态(实例变量),因为您提到了“非抽象 getter”,但您也在后代中定义了这样的关系。由于您的问题与此无关,因此我不会“批评”,但也许会回顾一下您为何如此拆分它。

关于java - OneToMany - org.hibernate.AnnotationException : mappedBy reference an unknown target entity property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17125713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com