I have a Hibernate @Embeddable annotated class like so
@Embeddable
public class DataWrapper {
private DataWrapperType wrapperType;
@ManyToOne
private DataFactorX xFactor;
@ManyToOne
private DataFactorY yFactor;
@ManyToOne
private DataFactorZ zFactor;
// Getters and Setters
}
and it’s pretty simply used in another entity
@Entity
public class DataManager {
private DataWrapper dataWrapper;
public DataManager() {
this.dataWrapper = new DataWrapper();
}
}
but when I later retrieve a DataManager instance from Hibernate, the dataWrapper field comes back null.
Any explanation?
I have the answer now, but I went ahead and posted because it was not obvious to me as a Hibernate amateur. Based on this answer by ChssPly76, I now see that Hibernate will set a component to
nullwhen all of it’s fields arenull. This behavior is official and documented I found out here:This means that while you’re still working with the reference that you set the field on, it will stay set, but if you query Hibernate to retrieve this object again, that field will be null if all of its components are null.
Hope that helps someone else not chase
NullPointerExceptions through otherwise working Java code.