3

我正在尝试将字节码增强添加到基于 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)更有效。但是,也许我错了,因为这个错误即将出现?

有没有更好的方法来注释我的两个模型实体?或者是否有我缺少的注释/选项将向字节码增强表明多对一关系完全是单向的,而不是双向的?

4

1 回答 1

0

这只是一个信息级别的警告。

enableAssociationManagement 确定“是否应该对双向关联管理进行增强”。因为它是单向的,所以没有工作要做。

当指定 failOnError 时,这不会停止您的构建。

如果您查看 PersistentAttributesEnhancer PersistentAttributesEnhancer的来源,您可以看到在未指定 mappedBy 时记录了此信息级别日志。

如果您的构建基于您的 failOnError 设置失败,则很可能存在不同的问题。错误消息不是很具有描述性,看起来像是信息消息是问题所在。我不得不在 IllegalStateException 上放置一个异常断点来确定真正的问题。

于 2019-08-15T18:35:22.817 回答