2016-01-25 94 views
0

這是失敗的:JPA的EntityManager的createQuery()錯誤

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: select typeaction0_.id as id1_102_, typeaction0_.libelle as libelle3_102_, typeaction0_.code as code4_102_, typeaction0_.type as type5_102_ from apex.typeActionCommerciale typeaction0_ where typeaction0_.type<>?

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

  • No value specified for parameter 1 org.hibernate.exception.DataException: could not extract ResultSet at

我使用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(); 
} 
+0

嘿傢伙,也許你必須使用setProperties方法。試試! –

+0

查詢很好。該問題指出參數1丟失。 TypeActionCommercialeEnum.NIP在哪裏?這就像它不在類路徑中一樣。你可以嘗試「從TypeActionCommercialeImpl類型!= TypeActionCommercialeEnum.NIP」(並刪除setParameter行),以查看你得到的錯誤 –

+0

@IanMc錯誤:'org.hibernate.QueryException:無法解析路徑[TypeActionCommercialeEnum.NIP],意外標記[TypeActionCommercialeEnum] [來自com.metier.impl.TypeActionCommercialeImpl,其中類型!= TypeActionCommercialeEnum.NIP] ' – Mercer

回答

0

我解決我的PB我有一個類public class EnumUserType<E extends Enum<E>> 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()); 
    } 

} 
0

嘗試使用<> inst的!= EAD這樣的:

"from TypeActionCommercialeImpl where type <> :type1" 
+0

它不起作用bro' – Mercer

+0

使用setProperties方法替代setParameter。並更改=到<> –

+0

@LuanOliveira同樣的錯誤,我更新我的帖子 – Mercer

1

的問題是在這裏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. 你可以存儲到數據庫的整數(也就是更小),而不是表示枚舉的字符串。
  2. 在存儲和檢索對象期間將解析委託給休眠。
  3. 可以使用枚舉直接進入查詢(像你正在嘗試做的)
+0

它不工作 – Mercer

+0

字段「類型」是什麼類型? –

+0

'type character varying(255)' – Mercer

相關問題