I am working with Hibernate and dto,dao design patterns (Java).
i have an entity class and attribute in it and they are defined with @ManyToOne
annotation.
I would like to create a count query and "tell" hibernate "DO NOT JOIN" with @ManyToOne
tables
While creating a count query:
(Long) crit.setProjection(Projections.count("id")).uniqueResult();
The sql exceute by hibernate is with left join
in it .
Even if i count explicitly on the @Id
annotation from the entity class .
The actual query appear is with LEFT JOIN for all the "other tables".
That SQL query build by Hibernate - is not efficient since there is no reason for creating a left join when @ManyToOne
is set.
After trying and reading about hibernate i found out about FetchMode
but even when setting FetchMode.LAZY
.setFetchMode("brand", FetchMode.LAZY)
The Sql from hiberante having left join in it.
i have also attached the photo from debug that showing all the SubcriteriaList which is under projectionCriteria .
How could i tell Hibernate DON'T left join Tables from entity class ? (without writing SQL query by myself) ?
public Response findAll() {
Criteria crit = getDtoCriteria();
}
public Criteria getDtoCriteria() {
return getDtoCriteria(getDtoClass(), getSession());
}
public Criteria getDtoCriteria(Class clazz, Session session) {
Criteria crit = createEntityCriteria(session);
setProjecttionForDto(crit, true, clazz);
return crit;
}
This all are my Subcriteria
[Subcriteria(bran*******ance:bran*******ance), Subcriteria(buc*****:buc*****), Subcriteria(br****:br****), Subcriteria(dyn***:dyn***), Subcriteria(dyna*****.user:user)]
Try using FetchMode.SELECT
instead:
(Long) crit.setFetchMode("brand",FetchMode.SELECT)
.setProjection(Projections.count("id"))
.uniqueResult();