简体   繁体   中英

DetachedCriteria isNotEmpty for Oracle

I discovered following problem today.

As I am using detached criteria, like detachedCriteria.add(Restrictions.ne(field, value)); to check if a String is empty. I found out that this is not working for oracle.

As a "IS NOT NULL"-Check is not enough for me, I tried to replicate the

WHERE length(trim(COL_NAME)) > 0

pattern working on Oracle like:

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

Now I get following criterion entriy after that (debug inspection):

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

where testtable is the table name, name is an @Embedded class and the property de is a property of the @Embedabble .

it finally results in a 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)"...

Without any message. Can anybody explain me what I am doing wrong?

Hibernate: 4.3.6.FINAL

According to the documentation I found the answer myself:

sqlRestriction
public static Criterion sqlRestriction(String sql)

Apply a constraint expressed in SQL. Any occurrences of {alias} will be replaced by the table alias.

Parameters:
sql -
Returns:
Criterion

The I've got the conclution my code should look like:

/**
 * 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;
}

where the sqlField is the NAME of the column in the DATABASE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM