Hibernate查询限制:NULL或修剪空白空字符串

问题描述:

添加的子-表问题Hibernate查询限制:NULL或修剪空白空字符串

我的一个Hibernate查询的限制是使用NULL或修剪空字符串

以下OR - TRIM(..)的限制不起作用,因为它不是SQLRestriction

final Criteria query = sessionFactory.getCurrentSession().createCriteria(PeopleT.class); 
query.add(Restrictions.or(
     Restrictions.eq("TRIM(childTable.commentText)",""), 
     Restrictions.isNull("childTable.commentText")) 
); 

我得到的错误是

org.hibernate.QueryException: could not resolve property: TRIM(

但我可以有一个或部分SQLRestriction,另一只是一个简单的限制?这是处理这个测试的方法吗?注意:我在这里有一个子表。

+0

请告诉我你的数据库? – Emad

+0

Oracle 12c ...... –

+0

这适用于MySQL,但不是所有数据库都有TRIM()函数。其他数据库有LTRIM()和RTRIM(),所以你必须把它叫做LTRIM(RTRIM(...))。 – Emad

试试这个:

add(Restrictions.sqlRestriction("{alias}.TRIMcommentText) = ?", ""), StringType.INSTANCE)) 
+1

我更新了我的帖子,问题是子表,别名不匹配,例如ChildTable.commentText到Hibernate的childtabl1_.commentText。 –