JPA 2 one-to-one fetch=FetchType.EAGER not working
In my UserTbl entity, I have this
@OneToOne( cascade = CascadeType.REMOVE, mappedBy = "user", fetch=FetchType.EAGER )
private RetailPostUserTbl retailPostUser;
@OneToOne( cascade = CascadeType.REMOVE, mappedBy = "user", fetch=FetchType.EAGER )
private Back开发者_StackOverflowOfficeUserTbl backOfficeUser;
But both are ALWAYS null.
A user must belong to at least one of them or both but the query always return null.
I tried fetching it via the criteria api like this
Root<UserTbl> root = cq.from( UserTbl.class );
root.fetch( UserTbl_.retailPostUser, JoinType.LEFT );
root.fetch( UserTbl_.backOfficeUser, JoinType.LEFT );
But they are still always null.
Any ideas why this is not working?
The reverse works fine, for eg
in RetailPostUserTbl, this works
@OneToOne( optional = false, cascade=CascadeType.REMOVE )
@PrimaryKeyJoinColumn
private UserTbl user;
You must be doing something wrong while saving or retrieving the entity, for @OneToOne mapping the FetchType is by default EAGER and you don't have to mention it explicitly.
Have you tried to load the entity by -
entityManager.find();
if above method works and returns the entity with all the mapped objects then check your criteria query.
精彩评论