最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why the alias is missing after unwrapping a JPA criteria query to Hibernate query? - Stack Overflow

programmeradmin3浏览0评论

I'm building a JPA criteria query but would like to use the setResultTransformer from Hibernate. As I understand I can first unwrap the JPA criteria query to a Hibernate query, then I can call the setResultTransformer API. This is fine, however in the transformTuple method in my result transformer class, the alias array is null even though I've specified alias when I build the criteria query. Why the alias name is missing?

Here is the code snippet. I'm using Hibernate 5.3.

        Session session = HibernateUtil.getCurrentSession();
        CriteriaBuilder cb = session.getCriteriaBuilder();
        CriteriaQuery<Tuple> cq = cb.createTupleQuery();
        Root<MyPDO> root = cq.from(MyPDO.class);
        
        cq.multiselect(
            root.get("col1").alias("alias1"),
            root.get("col2").alias("alias2")
        );

        TypedQuery<Tuple> query = session.createQuery(cq);
        query.setHint(".hibernate.cacheMode", CacheMode.IGNORE);
        
        MyResultTransformer myTransformer = new MyResultTransformer();
        List<MyPDO> results = query.unwrap(.hibernate.query.Query.class)
                .setResultTransformer(myTransformer)
                .getResultList();

Here is the definition for my result transformer:

private static class MyResultTransformer implements ResultTransformer {

        public Object transformTuple(Object[] tuple, String[] aliases) {
            
            MyPDO row = new MyPDO();
            //convert tuple to MyPDO here
            .
            .
            .
            return row;
        }
        
        
        public List transformList(List results) {
            return results;
        }
    }

In the transformTuple method, the input parameter tuple has the proper data for the result row, however the String array aliases contains null values for each column. I expect the aliases should have the alias names I have specified when building the query. Why are the aliases missing?

发布评论

评论列表(0)

  1. 暂无评论