I have three entities with one parents has two children, but I only listed two entities here.
@Entity
@Table(name = "person")
public class Person{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "name")
private String name;
@OneToMany(fetch=FetchType.EAGER)
private Set<Phone> phones = new HashSet<Phone>();
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="person_id", referencedColumnName="id")
private List<Book> books = new ArrayList<Book>();
}
@Entity
@Table(name = "book")
public class Book{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name="person_id")
private Long personId;
}
When I run following Hibernate criteria and noticed from SQL generated log, it is joining two children - phones and books together. I don't have any relationship between these two children. Why does SQL query generated from the Hibernate criteria try to join unrelated two children? I expected joining happening between parents and children, not between children.
Criteria criteria = session.createCriteria(Person.class)
.add(Restrictions.in("id", ids))
.setFetchMode("phones", FetchMode.JOIN).setFetchMode("books", FetchMode.JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
result = criteria.list();