1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Patrick
  • 2,672
  • 3
  • 31
  • 47

1 Answers1

2

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 null when all of it's fields are null. This behavior is official and documented I found out here:

The null value semantics of a component are ad hoc. When reloading the containing object, Hibernate will assume that if all component columns are null, then the entire component is null. This is suitable for most purposes.

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.

Community
  • 1
  • 1
Patrick
  • 2,672
  • 3
  • 31
  • 47