【问题标题】:JPA EntityManager createQuery() errorJPA EntityManager createQuery() 错误
【发布时间】:2016-05-02 08:20:04
【问题描述】:

这失败了:

public List<TypeActionCommerciale> requestTypeActionCommercialeSansNip() throws PersistenceException {
    Query query = createQuery("from TypeActionCommercialeImpl where type != :type1");
    query.setParameter("type1", TypeActionCommercialeEnum.NIP);
    return (List<TypeActionCommerciale>) query.list();
}

例外:

Hibernate:选择 typeaction0_.id 作为 id1_102_,typeaction0_.libelle 作为 libelle3_102_,typeaction0_.code 为 code4_102_,typeaction0_.type 为 type5_102_ 来自 apex.typeActionCommerciale typeaction0_ 其中 typeaction0_.type?

错误 org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions(SqlExceptionHelper.java:129)

  • 没有为参数 1 org.hibernate.exception.DataException 指定值:无法在以下位置提取 ResultSet

我使用 setProperties 但我有同样的错误:

public List<TypeActionCommerciale> requestTypeActionCommercialeSansNip() throws PersistenceException {
    Query query = createQuery("from TypeActionCommercialeImpl where type  <> :type1");
    final Map<String, Object> properties = new HashMap<>();
    properties.put("type1", TypeActionCommercialeEnum.NIP);
    query.setProperties(properties);
    return (List<TypeActionCommerciale>) query.list();
}

【问题讨论】:

  • 嘿家伙,也许你必须使用 setProperties 方法。试试看!
  • 查询良好。问题表明缺少参数 1。 TypeActionCommercialeEnum.NIP 在哪里?就像它不在类路径中一样。您可以尝试“from TypeActionCommercialeImpl where type != TypeActionCommercialeEnum.NIP”(并删除 setParameter 行)来查看您遇到的错误
  • @IanMc 错误:org.hibernate.QueryException: Unable to resolve path [TypeActionCommercialeEnum.NIP], unexpected token [TypeActionCommercialeEnum] [from com.metier.impl.TypeActionCommercialeImpl where type != TypeActionCommercialeEnum.NIP]
  • 更好。现在您可以专注于真正的问题了。
  • 如何在Entity类中定义类型?

标签: java hibernate postgresql jpa


【解决方案1】:

我解决了我的 pb 我有一个类 public class EnumUserType&lt;E extends Enum&lt;E&gt;&gt; implements UserType 并且我实现了这个方法:

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
     String name = rs.getString(names[0]);  
        Object result = null;  
        if (!rs.wasNull()) {  
            result = Enum.valueOf(clazz, name);  
        }  
        return result;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    if (null == value) {  
        st.setNull(index, Types.VARCHAR);  
    } else {  
        st.setString(index, ((Enum<E>) value).name());  
    }  

} 

【讨论】:

    【解决方案2】:

    问题出在这里 query.setParameter("type1", TypeActionCommercialeEnum.NIP);

    枚举类型未在休眠中定义,因此您必须存储枚举的名称并将其用于查询(简单的方法)然后使用:

    query.setString("type1", TypeActionCommercialeEnum.NIP.name());
    

    要直接使用枚举(硬方式),您必须实现您的 CustomUserType 。你可以在这里找到https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch06.html#types-custom

    使用CustomUserType的主要优点是:

    1. 您可以在 DB 中存储一个整数(更小),而不是代表枚举的字符串。
    2. 在存储和检索对象期间将解析委托为休眠。
    3. 您可以在查询中直接使用枚举(就像您尝试做的那样)

    【讨论】:

    • 字段“类型”是什么类型?
    • type character varying(255)
    • 问题是关键字“type”是private关键字。请您尝试更改测试的字段名称吗?
    • 它在 Hibernate 3 下工作,我在 Hibernate 5 下
    • 这是一个很奇怪的问题...尝试使用 setString() 并粘贴堆栈跟踪
    【解决方案3】:

    尝试像这样使用&lt;&gt; 而不是!=

      "from TypeActionCommercialeImpl where type <> :type1"
    

    【讨论】:

    • 使用 setProperties 方法代替 setParameter。并将 != 更改为
    • The method getResultList() is undefined for the type Query
    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 2012-04-12
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2011-12-03
    相关资源
    最近更新 更多