简体   繁体   English

使用Enum作为参数的Hibernate查询

[英]Hibernate Query using Enum as Parameter

I'm having no luck getting a hibernate (using HSQLDB) query to work. 我没有运气来使用hibernate(使用HSQLDB)查询。 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.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

The class is annotated like so: 该类注释如下:

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

...and the Enum type looks like: ......和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;
    }
}

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. 根据我对Hibernate文档的理解,这应该完全有效 - 更不用说我目前能够使用其他hibernate方法保存,编辑,删除,查询(通过id)对象 - 但“createQuery”证明是顽固的。

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. 似乎默认情况下,表是使用序数值(在我的情况下为CurrencyType.ordinal() )创建的,因此在创建表时,列最终看起来像CURRENCY INTEGER NOT NULL

This is less than ideal as if I change the Enum type (ie 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)来保存String值( CurrencyType.name() ),所以它看起来像:

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

...now to re-enter all the data. ...现在重新输入所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM