Hibernate 字节码增强 单向 多对一

4
我正在尝试为基于Java的Hibernate应用程序添加字节码增强。Hibernate版本为5.2.6.Final,它是在maven中构建的,因此我使用hibernate-enhance-maven-plugin。我已经测试了以下问题直到5.2.18.Final,但结果仍然相同。
"enableAssociationManagement"选项给我带来了几个问题,应用程序无法增强。我的问题是,我有多个单向ManyToOne映射,我只需要从子类访问父级即可。我从不需要从父实体引用子项。一个典型的映射看起来像这样:
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)。但是,也许我错了,因为这个错误出现了?

是否有更好的方法来注释我的两个模型实体?或者是否有一个注释/选项,我错过了,可以指示字节码增强,表示ManyToOne关系完全是单向的,而不是双向的?

1个回答

0

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

enableAssociationManagement决定“是否进行双向关联管理增强”。由于它是单向的,所以没有工作需要完成。

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

如果您查看PersistentAttributesEnhancerPersistentAttributesEnhancer的源代码,您可以看到当未指定mappedBy时,记录了此信息级别日志。

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


好的,即使failOnError属性被设置为true,构建仍然失败。那些消息是我在控制台中看到的唯一内容,所以我会按照您的建议断点其他错误,看看还发生了什么。谢谢。 - Curtis Snowden
@CurtisSnowden 追踪它? - user472749

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接