这是失败的:
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_,从apex.typeActionCommerciale typeaction0_选择typeaction0_.type作为type5_102_ typeaction0_.type<>? 误差org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions(SqlExceptionHelper.java:129)
我使用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();
}发布于 2016-01-27 22:22:37
我解析我的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());
}
} 发布于 2016-01-25 21:46:30
问题在这里,query.setParameter("type1",TypeActionCommercialeEnum.NIP);
枚举类型在hibernate中没有定义,因此必须存储enum的名称并将其用于查询(简单的方法),然后使用:
query.setString("type1", TypeActionCommercialeEnum.NIP.name());要直接使用枚举(困难的方法),您必须实现您的CustomUserType。你可以在这里找到https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch06.html#types-custom
使用CustomUserType的主要优点是:
发布于 2016-01-25 21:34:32
尝试使用<>而不是!=,如下所示:
"from TypeActionCommercialeImpl where type <> :type1"https://stackoverflow.com/questions/35002509
复制相似问题