Hibernate + Derby: Comparisons between 'BOOLEAN' and 'INTEGER' are not supported

前端 未结 4 1785
执笔经年
执笔经年 2021-01-13 16:32

I have a problem with querying Derby database. I am using Hibernate with JPA. Problem is related (probably) to boolean columns. Each query ends with error:

org.h

相关标签:
4条回答
  • 2021-01-13 17:11

    You have to set your boolean as a parameter:

    String queryString = "SELECT t FROM Task t WHERE t.deleted = :trueValue";
    Query query = entityManager.createQuery(queryString);
    query.setParameter("trueValue", true);
    query.getResultList();
    

    Hope it will work for you ;)

    0 讨论(0)
  • 2021-01-13 17:12

    Used Rafal's approach but also have to add

    @Override
    public String toBooleanValueString(boolean bool) {
        return bool ? "1" : "0";
    }
    

    in my Dialect. Otherwise it doesn't work under Hibernate 4.2.8.

    Also I wasn't be able to specify my dialect explicitly in persistence.xml as my app runs against Oracle and Derby. So I've added a dialect resolver for my dialect:

    <property name="hibernate.dialect_resolvers" value="...DerbyBoolAsIntDialectResolver"/>
    

    Resolver itself:

    public class DerbyBoolAsIntDialectResolver extends AbstractDialectResolver {
        @Override
        protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
            String databaseName = metaData.getDatabaseProductName();
            int databaseMajorVersion = metaData.getDatabaseMajorVersion();
    
            if ( "Apache Derby".equals( databaseName ) ) {
                final int databaseMinorVersion = metaData.getDatabaseMinorVersion();
                if ( databaseMajorVersion > 10 || ( databaseMajorVersion == 10 && databaseMinorVersion >= 7 ) ) {
                    return new DerbyBoolAsIntDialect();
                }
            }
            return null;
        }
    }
    
    0 讨论(0)
  • 2021-01-13 17:27

    Downgrade didn't helped. Here is valid solution which works for me:

    import java.sql.Types;
    import org.hibernate.dialect.DerbyTenSevenDialect;
    
    public class DerbyDialect extends DerbyTenSevenDialect {
    
        public DerbyDialect() {
            // fix Derby dialect boolean data type mapping error
            registerColumnType(Types.BOOLEAN, "INTEGER");
        }
    }
    

    I guess it is better to configure 'true' and 'false' constants mapping in dialect to Derby data type, but above is good enough for now.

    0 讨论(0)
  • 2021-01-13 17:32

    Boolean datatype was added in Derby 10.7, you appear to be using 10.7 or newer so you should use the org.hibernate.dialect.DerbyTenSevenDialect dialect which adds Boolean support.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题