Consider the following Parent class that has two ManyToOne references.
@Entity
@Table(name = "PARENT")
public class Parent {
private static final long serialVersionUID = 3730163805206219313L;
@Id
@SequenceGenerator(name = "SEQ#PARENT", sequenceName = "SEQ#PARENT", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ#PARENT")
@Column(name = "ID")
private long id;
@ManyToOne(optional = false, fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="CHILD_1_ID", referencedColumnName="FK_COLUMN_NAME")
private Child childInstance1;
@ManyToOne(optional = false, fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="CHILD_2_ID", referencedColumnName="FK_COLUMN_NAME")
private Child childInstance2;
@Column(name = "USER_ID")
private String userId;
}
I am using the following hql query to load the Parent instance:
"from Parent p join fetch p.childInstance1 join fetch p.childInstance2 where p.userId = :userId"
This always results in hibernate issuing separate sql statements to load the childInstance1 rows even though it does the fetch join.
Any help in avoiding the extra sql statements is appreciated.