我没有运气来使用hibernate(使用HSQLDB)查询。查询代码如下所示:
Query query = session.createQuery("from "+tableName+" where CURRENCY = :currency");
query.setParameter("currency",currency);
List<ExchangeRate> list = query.list();
我一直得到“引起:org.hsqldb.HsqlException:转换中的数据类型不兼容”:
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.loader.Loader.doList(Loader.java:2529)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1275)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at com.rockymountaineer.wsapi.db.test.ExchangeRateDAOTest.getRate(ExchangeRateDAOTest.java:27)
at com.rockymountaineer.wsapi.db.test.ExchangeRateDAOTest.main(ExchangeRateDAOTest.java:39)
Caused by: java.sql.SQLSyntaxErrorException: incompatible data type in conversion
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.throwError(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setBytes(Unknown Source)
at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$1.doBind(VarbinaryTypeDescriptor.java:57)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:612)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
... 10 more
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.types.NumberType.convertToDefaultType(Unknown Source)
... 24 more
该类注释如下:
@Column(name="CURRENCY", nullable=false)
public CurrencyType getCurrency() {
return currency;
}
...和Enum类型看起来像:
public enum CurrencyType {
CAD, AUD, EUR, GBP, USD;
/**
* @param currency
* @return
*/
public static CurrencyType getByCurrency(String currency) {
if(currency!=null) {
for(CurrencyType type : CurrencyType.values()) {
if(type.name().equals(currency)) return type;
}
}
return null;
}
}
根据我对Hibernate文档的理解,这应该完全有用 - 更不用说我目前能够使用其他hibernate方法保存,编辑,删除,查询(通过id)对象 - 但是“createQuery”正在证明顽固。
如果有人可以提供帮助,我会真诚地感激它! 欢呼声,
亚历
答案 0 :(得分:8)
......好吧 - 想通了。如果其他人感到困惑,这就是我发现的。似乎默认情况下,表是使用序数值创建的(在我的情况下为CurrencyType.ordinal()
),因此在创建表时,列最终看起来像CURRENCY INTEGER NOT NULL
。
这不太理想,好像我改变了枚举类型(即值的顺序),这将破坏一切。好消息是我可以通过向方法添加String
来强制它保存CurrencyType.name()
值(@Enumerated(EnumType.STRING)
),所以它看起来像:
@Enumerated(EnumType.STRING)
@Column(name="CURRENCY", nullable=false)
public CurrencyType getCurrency() {
return currency;
}
...现在重新输入所有数据。