【发布时间】:2019-12-16 16:19:00
【问题描述】:
我正在尝试将字节码增强添加到基于 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; }
}
但如前所述,我不想要一个。我目前的理解是它是more efficient to only have a unidirectional @ManyToOne mapping(只是@ManyToOne)。但是,也许我错了,因为这个错误即将到来?
有没有更好的方法来注释我的两个模型实体?或者是否有我缺少的注释/选项会向字节码增强表明多对一关系完全是单向的,而不是双向的?
【问题讨论】:
标签: java hibernate maven byte-code-enhancement