DetachedCriteria是Oracle的NoEmpty

时间:2018-01-15 15:56:39

标签: oracle hibernate restrictions detachedcriteria

我今天发现了以下问题。

因为我正在使用分离标准,例如detachedCriteria.add(Restrictions.ne(field, value));来检查String是否为空。我发现这不适用于oracle。

作为一个" IS NOT NULL" -Check对我来说还不够,我试图复制

WHERE length(trim(COL_NAME)) > 0

在Oracle上工作的模式如下:

detachedCriteria.add(Restrictions.sqlRestriction(String.format("length(trim({%s}.%s)) > 0", aliasName, field)));

现在我得到了以下标准(调试检查):

length(trim({testtable}.name.de)) > 0

其中testtable是表名,name@Embedded类,属性de@Embedabble的属性。

最终导致NullPointerExcpetion ....

Method threw 'java.lang.NullPointerException' exception.
0 = {StackTraceElement@32701} "oracle.jdbc.driver.T4C8Oall.getNumRows(T4C8Oall.java:879)"
1 = {StackTraceElement@32702} "oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:881)"
2 = {StackTraceElement@32703} "oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145)"
3 = {StackTraceElement@32704} "oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267)"
4 = {StackTraceElement@32705} "oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)"
5 = {StackTraceElement@32706} "oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493)"
6 = {StackTraceElement@32707} "oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)"
7 = {StackTraceElement@32708} "com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:174)"
8 = {StackTraceElement@32709} "org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)"
9 = {StackTraceElement@32710} "org.hibernate.loader.Loader.getResultSet(Loader.java:2065)"...

没有任何消息。谁能解释一下我做错了什么?

Hibernate:4.3.6.FINAL

1 个答案:

答案 0 :(得分:0)

根据文件,我自己找到了答案:

  

sqlRestriction
  public static Criterion sqlRestriction(String sql)

     

应用SQL表示的约束。任何{alias}都会出现   由别名替换。

     

参数:
  sql -
  返回:
  标准

我得到的结论应该是我的代码:

/**
 * Adds a constraint to the where clause checking if the specified field value's length is bigger than ZERO
 * @param sqlField the field of the table the query belongs to (in SQL Syntax not hql or something else)
 * @return Query
 */
public final Query<T> isNotEmptySQLField(String sqlField){
    //Apply a constraint expressed in SQL. Any occurrences of {alias} will be replaced by the table alias.
    detachedCriteria.add(
          Restrictions.sqlRestriction(
              String.format("length(trim({alias}.%s)) > 0", sqlField)));
    return this;
}

其中sqlFieldNAME中列的DATABASE