我正在尝试将字节码增强添加到基于 Java 的 Hibernate 应用程序中。Hibernate 是 5.2.6.Final 版本,它内置在 maven 中,所以我使用的是hibernate-enhance-maven-plugin。我已经测试了以下问题直到 5.2.18.Final,但结果是一样的。
“enableAssociationManagement”选项给我带来了几个问题,应用程序无法增强。我的问题是我有几个单向多对一映射,我只需要从子类访问父类。我永远不需要引用来自父实体的子实体。
我拥有的典型映射如下所示:
public class Child implements Serializable {
private Parent parent;
@ManyToOne
@JoinColumn(name="FK_PARENT", referencedColumnName="ID", nullable=true)
public Parent getParent() { return this.parent; }
public void setParent(Parent parent) { this.parent = parent; }
}
public class Parent implements Serializable {
//No variable/getter/setter for a collection of the children, as they are not needed here.
}
所有这些映射在增强过程中都失败了,给我以下错误:
INFO: Enhancing [com.mycom.model.Child] as Entity
Aug 08, 2019 3:31:09 PM org.hibernate.bytecode.enhance.internal.javassist.PersistentAttributesEnhancer handleBiDirectionalAssociation
INFO: Could not find bi-directional association for field [com.mycom.model.Child#parent]
我理解错误。我在父级中没有相应的 @OneToMany 注释。我已经确认这确实有效:
public class Child implements Serializable {
private Parent parent;
@ManyToOne
@JoinColumn(name="FK_PARENT", referencedColumnName="ID", nullable=true)
public Parent getParent() { return this.parent; }
public void setParent(Parent parent) { this.parent = parent; }
}
public class Parent implements Serializable {
private Set<Child> children;
@OneToMany(mappedBy="parent")
public Set<Child>getChildren() { return this.children; }
public void setChildren(Set<Child>parent) { this.children = children; }
}
但如前所述,我不想要一个。我目前的理解是,只有一个单向的@ManyToOne 映射(只是@ManyToOne)更有效。但是,也许我错了,因为这个错误即将出现?
有没有更好的方法来注释我的两个模型实体?或者是否有我缺少的注释/选项将向字节码增强表明多对一关系完全是单向的,而不是双向的?