NimChimpsky
NimChimpsky

Reputation: 47310

eager fetch performs left join in hibernate

I am defining an entity, myParent, it works fine except for one thing. It has the following defined :

@OneToMany(mappedBy = "myParent", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
private List<Child> children;

This causes a left or right join to be performed so I get two parents if there are two children. I don't want that I just want one parent, and two children.

I could change fetchtype to lazy but I didn't really want that ... ?

when I select all parents the list contains duplicate parents, I do not want the duplicates.

Upvotes: 1

Views: 729

Answers (1)

darrengorman
darrengorman

Reputation: 13124

You should use the DISTINCT_ROOT_ENTITY ResultTransformer.

This means that each row of results is a distinct instance of the root entity.

Upvotes: 1

Related Questions