首页 > 解决方案 > org.hibernate.type.NumericBooleanType 不转换 JPASQLQuery 中的值

问题描述

在我的模型中,我声明了这个 NumericBooleanType 属性:

@Column(name = "ST_ATIVO", nullable = false)
@Type(type = "org.hibernate.type.NumericBooleanType")
private Boolean ativo;

然后,在另一个类中,我正在尝试执行以下查询:

List<Long> test = this.sqlQueryFactory.query()
            .select(equipe.id)
            .from(equipe)
            .where(equipe.ativo.isTrue())
            .fetch();

但是,我收到了这个错误:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = boolean

查询中的 NumericBooleanType 布尔属性不应该被休眠转换为 int 吗?

标签: javaspring-boothibernate

解决方案


目前尚不清楚您使用什么查询 api。假设您使用querydsl,您可以尝试以这种方式更正您的查询:

JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
List<Long> test = queryFactory.query()
   .select(equipe.id)
   .from(equipe)
   .where(equipe.ativo.eq(true))
   .fetch();

推荐阅读