pb00067 wrote:
usil wrote:
pb00067 wrote:
How dou you declare the OneToMany relation exactly?
Are you declaring EAGER fetch there?
No, it's declared as LAZY.
Then I cannot understand why your query implicitely makes inner join fetch to initialize collections of entities B.
Can you please post your code where you define and exectute both HQL queries.
Maybe I could explain it a little bit more on the simplified code. I would like not to put exact code here because it is company's code, so I changed the names a little bit.
Both queries as declared as namedQuery and entities are like this (assume that entity A means ServiceGroup and entity B means ServiceType):
Code:
@Entity
@Table(name = "SERVICE_GROUP")
@NamedQueries({
@NamedQuery(name = "ServiceGroup.findAllUsedAndStandardServicesById", query = "SELECT distinct d FROM ServiceGroup d inner join fetch d.serviceTypeCollection t where d.id=t.serviceGroupId and t.isStandard = 1 and t.isUsed=1 and d.id = :id"),
@NamedQuery(name = "ServiceGroup.findAllUsedAndExtendedServicesById", query = "SELECT distinct d FROM ServiceGroup d inner join fetch d.serviceTypeCollection t where d.id=t.serviceGroupId and t.isExtended = 1 and t.isUsed=1 and d.id = :id")})
public class ServiceGroup {
@Id
@Column(name = "ID")
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy = "serviceGroupId")
private Set<ServiceType> serviceTypeCollection;
}
@Entity
@Table(name = "SERVICE_TYPE")
public class ServiceType {
@Id
@Column(name = "ID")
private Long id;
@JoinColumn(name = "SERVICE_GROUP_ID", referencedColumnName = "ID")
@ManyToOne(optional = false)
private ServiceGroup serviceGroupId;
}
Both queries are called from code like this which implements particular use case:
Code:
Query query = em.createNamedQuery(namedQueryName);
query.setParameter(parameterName, parameterValue);
query.getResultList();
where em is of course EntityManager.
The parameterValue is the same for both queries (the same id of ServiceGroup entity). The difference in these both queries is condition: t.isStandard = 1 in first query and t.isExtended = 1 in second one.
The LAZY fetch type is set because in some other use cases there is no need to retrieve serviceTypeCollection.
I hope this helps. If I cut the code too much and more details are needed, let me know. Thanks