I'm attempting to add Bytecode Enhancement to a Java-based Hibernate application. Hibernate is version 5.2.6.Final, and it's built in maven, so I'm using the hibernate-enhance-maven-plugin. I have tested the following issue up to 5.2.18.Final, but the results have been the same.
The "enableAssociationManagement" option is giving me several problems and the application fails to enhance. My problem is that I have several unidirectional ManyToOne mappings, where I only need access to the parent from the child class. I never need to have a reference to children from the parent entity.
A typical mapping that I have looks like this:
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.
}
All of these mappings fail during the enhancement, giving me the following errors:
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]
I understand the error. I don't have a corresponding @OneToMany annotation in the parent. I have confirmed that this does work:
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; }
}
But as stated, I don't desire one. My current understanding is that it's more efficient to only have a unidirectional @ManyToOne mapping (Just @ManyToOne). But, maybe I'm wrong because this error is coming up?
Is there a better way that I should annotate my two model entities? Or is there an annotation/option that I'm missing that will indicate to Bytecode Enhancement that a ManyToOne relationship is fully intended to be unidirectional, and not bi-directional?