我在查询Derby数据库时遇到问题。我在JPA中使用Hibernate。问题与(可能)与布尔列有关。每个查询以错误结尾:
org.hibernate.exception.SQLGrammarException:不支持’BOOLEAN’和’INTEGER’之间的比较。 类型必须是可比较的。字符串类型还必须具有匹配的排序规则。如果排序规则不匹配,则可能的解决方案是强制转换操作数以将其强制为默认排序规则(例如,从sys.systables WHERE CAST(tablename AS VARCHAR(128))=’T1’的SELECT表名)
您可以在下面找到示例代码和配置。样本经过简化,以便于阅读。这是我的JPA实体:
@Entity public abstract class Task implements Serializable { @Id @GeneratedValue(strategy = GenerationType.TABLE) protected long id; @Column protected boolean deleted; public long getId() { return id; } public boolean isDeleted() { return deleted; } public void setId(long id) { this.id = id; } public void setDeleted(boolean deleted) { this.deleted = deleted; } @Override public int hashCode() { return id; } @Override public boolean equals(Object object) { if (object == null) { return false; } if (!this.getClass().equals(object.getClass())) { return false; } EntityObject other = (EntityObject) object; if (this.id != other.id) { return false; } return true; } @Override public String toString() { return "EntityObject[ id=" + id + " ]"; } }
我的JPA查询:
SELECT t FROM Task t WHERE deleted = false
我的JPA配置:
<persistence-unit name="PU1" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>jdbc/myapp</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/> </properties> </persistence-unit>
怎么了?如何解决?感谢您的任何建议。
降级没有帮助。这是对我有效的有效解决方案:
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"); } }
我想最好将方言中的“ true”和“ false”常量映射配置为Derby数据类型,但是上面的内容就足够了。