首页 新闻 会员 周边

Hibernate的Restrictions.sqlRestriction中如何使用别名?

0
悬赏园豆:50 [已关闭问题] 关闭于 2016-02-03 15:20

查看api文档

public static Criterion sqlRestriction(String sql,
Object value,
Type type)
Apply a constraint expressed in SQL, with the given JDBC parameter. Any occurrences of {alias} will be replaced by the table alias.

{alias}在输出的sql语句中为this,那么如何使用关联的其他表呢?

DetachedCriteria dc = collectDao.createDetachedCriteria();
dc.createAlias("requestUser", "user"); dc.createAlias("user.company", "cp"); dc.add(Restrictions.like("cp.name", "%" + cpName + "%"));

或者

dc.add(Restrictions.sqlRestriction("{alias}.title like ? escape'/'", StringUtil.escapeSQLLike(title), StringType.INSTANCE));

如上这样的写法是都是没有问题的。

但如何在Restrictions.sqlRestriction方法中使用cp.name呢?(因为要对特殊字符进行处理,所以此处要将Restrictions.like方法换为Restrictions.sqlRestriction)

尝试了{cp}.name,cp.name都行不通。

希望大神给解答



姑射蝉的主页 姑射蝉 | 菜鸟二级 | 园豆:204
提问于:2016-02-02 14:35
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册