9

I have a spring boot 2.7.5 with hibernate 5.6.12 project and I wanted to upgrade to spring boot 3.1.0 with hibernate 6.6.2 I'm trying a query count in database for getting total number of records in database an I get the error

org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup - model.dao.User(1057834329991800) at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.prepareReusablePath(BaseSqmToSqlAstConverter.java:3349) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]

Base class is:

public class JPADaoImpl {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    @Qualifier("dataSource")
    protected DataSource dataSource;
    
    @PersistenceContext
    protected EntityManager entityManager;
    
    protected <T> PageData<T> getPageData(CriteriaQuery<T> criteria,Root<T> root,Integer pStart, Integer pSize,LockModeType lockModeType) throws Exception{
        PageData<T> pageData =null;
        try {
            pageData = new PageData<T>();
            
            criteria.distinct(true);
            
            criteria.select(root);
            
            TypedQuery<T> query = getPersistenceQuery(criteria, pStart, pSize, lockModeType);
            
            if (pStart == null || pSize == null) {
                pageData.setData(query.getResultList());
                return pageData;
            }
            
            pageData.setpStart(pStart);
            pageData.setpSize(pSize);
            
            Long rowCount = getRowCount(criteria,root,true);
            pageData.setTotalNo(rowCount);

            pageData.setpCount(rowCount / pSize + (rowCount % pSize == 0 ? 0 : 1));
    
            pageData.setData(query.getResultList());
            
            return pageData;
        } catch (Exception e) {
            throw e;
        }
    }
    
    
    /**
     * 
     * @param criteria
     * @param root
     * @param distinct
     * @return
     * @throws Exception
     */
    protected <T> Long getRowCount(CriteriaQuery<T> criteria,Root<T> root,boolean distinct) throws Exception {
        Long rowcount=null;
        CriteriaBuilder builder = entityManager.getCriteriaBuilder(); 
        CriteriaQuery<Long> countCriteria=builder.createQuery(Long.class);
        
        Root<?> entityRoot = countCriteria.from(root.getJavaType());
        entityRoot.alias(root.getAlias());
        
        doJoins(root.getJoins(),entityRoot);
        if( criteria.isDistinct()){
             countCriteria.select(builder.countDistinct(entityRoot));
        }else{
            countCriteria.select(builder.count(entityRoot));
        }
        
        Predicate fromRestriction = criteria.getRestriction();
    
        if (fromRestriction != null) {
            countCriteria.where(fromRestriction);
        }
        
        countCriteria.distinct(criteria.isDistinct());
    
        rowcount=entityManager.createQuery(countCriteria).getSingleResult();

        return rowcount;
    }
    
    /**
     * 
     * @param joins
     * @param root_
     */
    private void doJoins(Set<? extends Join<?, ?>> joins,Join<?,?> root_){
        for(Join<?,?> join: joins){
            Join<?,?> joined = root_.join(join.getAttribute().getName(),join.getJoinType());
            doJoins(join.getJoins(),joined);
        }
    }
    /**
     * 
     * @param joins
     * @param root_
     */
    private void doJoins(Set<? extends Join<?, ?>> joins,Root<?> root_){
        for(Join<?,?> join: joins){
            Join<?,?> joined = root_.join(join.getAttribute().getName(),join.getJoinType());
            doJoins(join.getJoins(), joined);
        }
    }

    
        /**
     * @param criteria
     * @param pStart
     * @param pSize
     * @param lockModeType
     * @return
     */
    private <T> TypedQuery<T> getPersistenceQuery(CriteriaQuery<T> criteria, Integer pStart, Integer pSize, LockModeType lockModeType) {
        
        TypedQuery<T> query = entityManager.createQuery(criteria);
        
        if(lockModeType!=null){
            query.setLockMode(lockModeType);
        }
        
        if (pStart != null && pSize != null) {
            query.setFirstResult((pStart - 1) * pSize);
            query.setMaxResults(pSize);
        }
        return query;
    }

}

and the class that extends it

public class UserDaoImpl extends JPADaoImpl implements UserDao{
    
    @Override
    public PageData<User> list(User filter, Integer pStart, Integer pSize, List<Order> order, User userAuth) throws Exception {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<User> criteria = criteriaBuilder.createQuery(User.class);
        Root<User> root = criteria.from(User.class);
        root.alias("root");
        criteria.select(root);
    
        List<Predicate> predicateList = null;
                    
        if (filter!=null){
            
            predicateList = new ArrayList<Predicate>();
            
            if (!Utils.isEmpty(filter.getStatus())) {
                predicateList.add(criteriaBuilder.and(criteriaBuilder.equal(root.<Byte>get("status"),filter.getStatus())));
            }
        }
        
        if(!Utils.isEmpty(predicateList)){
            criteria.where(criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])));
        }
        criteria.orderBy(criteriaBuilder.asc(root.get("last_name")),criteriaBuilder.asc(root.get("first_name")));
        
        PageData<User> pageData;
    
        pageData = getPageData(criteria,root, pStart, pSize);
        
    
        return pageData;
    }

}

in UserDaoImpl.list method, JPADaoImpl.getRowCount at line rowcount=entityManager.createQuery(countCriteria).getSingleResult();

I get the error

org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup - model.dao.User(1057834329991800) at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.prepareReusablePath(BaseSqmToSqlAstConverter.java:3349) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]

If I comment line countCriteria.where(fromRestriction); it works ok

It works also in spring boot 2.7.5 with hibernate 5.6.12

Is it a bug in the new version of hibernate? Is it a workaround to fix this?

Thank you,
Adrian

Adrian
  • 101
  • 8

2 Answers2

0

In my case I changed the way to fill the predicate and it worked:

    Predicate[] predicates = {
        builder.like(builder.lower(root.get("colunm1")),"%" + numeroProcesso.toLowerCase() + "%"),
        root.get("colunm2").in(orgaoJulgadores)
    };

    criteria.where(predicates);

for

List<Predicate> predicates = new ArrayList<>();

predicates.add(builder.like(builder.lower(root.get("colunm1")),
            "%" + numeroProcesso.toLowerCase() + "%"));
predicates.add(root.get("colunm2").in(orgaoJulgadores));

criteria.where(predicates.toArray(new Predicate[predicates.size()]));
-1
Predicate fromRestriction = criteria.getRestriction();
// hibernate 5.x
if (fromRestriction != null) {
  countCriteria.where(fromRestriction);
}


 Predicate predicateList= new ArrayList();
 // hibernate 6.x
 if (predicateList!= null) {
   countCriteria.where(predicateList);
 }
Todd
  • 1
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '23 at 08:44
  • 1
    Hi, I've upgradet to spring boot 3.1.1 so I have hibernate core 6.2.5 but still getting the same error. Does anybody has a resolution? I am stuck with this. – Adrian Jun 26 '23 at 08:31
  • Hi Todd, in tour code for hibernate 6.x predicateList will always be !=null butt with size 0, obvious. Can you explain or extend your answer? Thank you! – Adrian Jun 26 '23 at 09:08