1

I have two entities that look similar this:

EntityA:

@Entity
@Getter
@Setter
@Table(name = "TABLE_A")
    public class EntityA implements SomeInterface {

    @Id
    @Column(name = "A_ID")
    private Long id;

    @OneToOne(mappedBy = "entityA")
    private EntityB entityB;

    @ManyToOne
    @JoinColumn(name = "A_C_ID")
    private EntityC entityC;

    ...
    some other fields

    @Override
    public EntityC retrieveEntityC() {
        return getEntityC();
    }
}

EntityB:

@Entity
@Getter
@Setter
@Table(name = "TABLE_B")
public class EntityB implements SomeInterface {

    @Id
    @Column(name = "B_ID")
    private Long id;

    @OneToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "B_A_ID", unique = true, nullable = false)
    private EntityA entityA;

    ...
    some other fields

    @Override
    public EntityC retrieveEntityC() {
        return getEntityA().retrieveEntityC();
    }
}

I have implemented Hibernate event listener of type POST_LOAD. When Hibernate tries to load EntityA, it triggers loading of its child - entityB - first. When entityB is loaded, the POST_LOAD event is fired and my event listener is beeing called (in context of EntityB). In my listener I call retrieveEntityC() method from entityB, which calls retrieveEntityC() method from entityA to finally get entityC from entityA. The problem is that I get NULL instead of exact entityC object. The entityA object is just a proxy, with only id field populated and other fields set to NULL. Calling a getter does not result in retrieving an object from database as I would suspect.

In this scenario, is there any way to make Hibernate actually get entityC from database?

mitrue
  • 63
  • 6

0 Answers0