本文介绍了使用枚举作为参数进行Hibernate查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有运气得到休眠(使用HSQLDB)查询工作。查询代码如下所示:

 查询查询= session.createQuery(from+ tableName +其中CURRENCY =:currency) ; 
query.setParameter(currency,currency);
列表< ExchangeRate> list = query.list();

我一直得到导致:org.hsqldb.HsqlException:转换中不兼容的数据类型:

  org.hibernate.exception.SQLGrammarException:无法在org.hibernate.exception.internal.SQLExceptionTypeDelegate执行查询
。 convert(SQLExceptionTypeDelegate.java:82)
在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert( SqlExceptionHelper.java:125)
在org.hibernate.loader.Loader.doList(Loader.java:2529)
在org.hibernate.loader.Loader.doList(Loader.java:2512)
在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
在org.hibernate.loader.Loader.list(Loader.java:2337)
在org.hibernate.loader .hql.QueryLoader.list(QueryLoader.java:495)
在org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:3 57)
在org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
在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)
导致:java.sql.SQLSyntaxErrorException:转换中不兼容的数据类型
在org.hsqldb.jdbc.JDBCUtil。 sqlException(未知源)
在org.hsqldb.jdbc.JDBCUtil.throwError(未知源)
在org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(未知源)
在org.hsqldb。 jdbc.JDBCPreparedStatement.setBytes(Unknown Source)
在org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor $ 1.doBind(VarbinaryTypeDescriptor.java:57)
在org.hibernate.type.descriptor.sql。 BasicBinder.bind(BasicBind er.java:93)
在org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
在org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
在org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
在org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:612)
在org.hibernate .loader.Loader.prepareQueryStatement(Loader.java:1875)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
在org.hibernate.loader.Loader.executeQueryStatement(Loader .java:1816)
在org.hibernate.loader.Loader.doQuery(Loader.java:900)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
在org.hibernate.loader.Loader.doList(Loader.java:2526)
... 10更多
导致:org.hsqldb.HsqlException:转换中不兼容的数据类型
at org.hsq ldb.error.Error.error(未知来源)
在org.hsqldb.error.Error.error(未知来源)
在org.hsqldb.types.NumberType.convertToDefaultType(未知来源)
... 24更多

类注释如下:

  @Column(name =CURRENCY,nullable = false)
public CurrencyType getCurrency(){
return currency;
}

...,枚举类型如下所示:

  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;
}
}
返回null;
}
}

根据Hibernate文档的理解,完全工作 - 更不用说我目前能够使用其他hibernate方法保存,编辑,删除,查询(通过id)对象 - 但是createQuery证明是固执的。



如果有人可以帮助我真诚的感谢!
Cheers,



Alex

解决方案

好吧 - 想出来如果有人感到困惑,这里是我发现的。看来,默认情况下,使用序数值创建表(在我的情况下 CurrencyType.ordinal()),所以列最终看起来像 CURRENCY INTEGER NOT NULL 表创建时。



这不如我更改Enum类型(即,价值),这将破坏一切。好消息是,我可以通过添加 String 值( CurrencyType.name())来强制它, code> @Enumerated(EnumType.STRING)到方法,所以它看起来像:

  @Enumerated(EnumType.STRING)
@Column(name =CURRENCY,nullable = false)
public CurrencyType getCurrency(){
return currency;
}

...现在重新输入所有数据。


I'm having no luck getting a hibernate (using HSQLDB) query to work. The query code looks like:

Query query = session.createQuery("from "+tableName+" where CURRENCY = :currency");
query.setParameter("currency",currency);
List<ExchangeRate> list = query.list();

I consistently get "Caused by: org.hsqldb.HsqlException: incompatible data type in conversion":

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

The class is annotated like so:

@Column(name="CURRENCY", nullable=false)
public CurrencyType getCurrency() {
    return currency;
}

...and the Enum type looks like:

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

From what I understand by the Hibernate documentation, this should totally work - not to mention that I am currently able to save, edit, delete, query (by id) objects using other hibernate methods - but the "createQuery" is proving stubborn.

If anyone can help I'd sincerely appreciate it!Cheers,

Alex

解决方案

...ok - figured it out. In case anyone else is confused, here is what I discovered. It seems that by default, the table is created using the ordinal value (in my case CurrencyType.ordinal()) so the column ends up looking like CURRENCY INTEGER NOT NULL when the table is created.

This is less than ideal as if I change the Enum type (i.e. the order of the values), this will break everything. The good news is I can force it to save the String value (CurrencyType.name()) by adding a @Enumerated(EnumType.STRING) to the method, so it will look like:

    @Enumerated(EnumType.STRING)
    @Column(name="CURRENCY", nullable=false)
    public CurrencyType getCurrency() {
        return currency;
    }

...now to re-enter all the data.

这篇关于使用枚举作为参数进行Hibernate查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:35