gpt4 book ai didi

java - Hibernate 字节码增强单向 ManyToOne

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:21 26 4
gpt4 key购买 nike

我正在尝试向基于 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; }
}

但如前所述,我不想要一个。我目前的理解是 more efficient to only have a unidirectional @ManyToOne mapping (只是@ManyToOne)。但是,也许我错了,因为这个错误即将出现?

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

最佳答案

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

enableAssociationManagement 确定“是否应增强双向关联管理”。由于它是单向的,因此无需做任何工作。

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

如果您查看 PersistentAttributesEnhancer 的源代码 PersistentAttributesEnhancer可以看到,当没有指定mappedBy时,会记录这个info级别的日志。

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

关于java - Hibernate 字节码增强单向 ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57421320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com