这是失败的:
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 as libelle3_102_,typeaction0_.code为code4_102_,typeaction0_.type为 type5_102_来自apex.typeActionCommerciale typeaction0_ where typeaction0_.type&LT;&GT ;?
ERROR 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();
}
答案 0 :(得分:1)
问题在于query.setParameter(“type1”,TypeActionCommercialeEnum.NIP);
enum类型未在hibernate中定义,因此您必须存储枚举的名称并将其用于查询(简单方法),然后使用:
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 :(得分:0)
尝试使用<>
代替!=
,如下所示:
"from TypeActionCommercialeImpl where type <> :type1"
答案 2 :(得分: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());
}
}