【发布时间】:2025-12-10 22:10:01
【问题描述】:
简而言之:hibernate 不支持投影和示例查询?我找到了这篇文章:
代码是这样的:
User usr = new User();
usr.setCity = 'TEST';
getCurrentSession().createCriteria(User.class)
.setProjection( Projections.distinct( Projections.projectionList()
.add( Projections.property("name"), "name")
.add( Projections.property("city"), "city")))
.add( Example.create(usr))
就像另一张海报说的那样,生成的 sql 一直有一个 where 类只引用 y0_= ?而不是 this_.city。
我已经尝试了几种方法,并搜索了问题跟踪器,但没有找到任何相关信息。
我什至尝试过使用投影别名和变形金刚,但它不起作用:
User usr = new User();
usr.setCity = 'TEST';
getCurrentSession().createCriteria(User.class)
.setProjection( Projections.distinct( Projections.projectionList()
.add( Projections.property("name"), "name")
.add( Projections.property("city"), "city")))
.add( Example.create(usr)).setResultTransformer(Transformers.aliasToBean(User.class));
有没有人通过示例使用过投影和查询?
【问题讨论】:
-
你能显示完整的生成的sql吗?
-
我添加了一个解决方案,其中包含我遇到类似问题时生成的 SQL。
标签: java hibernate criteria projection