【问题标题】:Hibernate Bytecode Enhancement Unidirectional ManyToOneHibernate 字节码增强单向多对一
【发布时间】: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


    【解决方案1】:

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

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

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

    如果您查看 PersistentAttributesEnhancer 的来源 PersistentAttributesEnhancer可以看到这个信息级别的日志是在没有指定mappedBy时记录的。

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

    【讨论】:

    • 好的,即使 failOnError 属性设置为 true,构建也会失败。这些消息是我在控制台中看到的唯一内容,所以我会按照你的建议做,并断点其他错误,看看还会发生什么。谢谢。
    • @CurtisSnowden 追踪它?
    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2023-02-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2020-07-22
    相关资源
    最近更新 更多