简体   繁体   中英

JPA EntityManager createQuery() error

This is failing:

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

exception:

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

i use setProperties but i have the same error:

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();
}

The problem is here query.setParameter("type1", TypeActionCommercialeEnum.NIP);

The enum type is not defined in hibernate so you must store the name of enum and use it for the query (the easy way) then use:

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

To use enum directly (the hard way) you must implement your CustomUserType . You can find here how https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch06.html#types-custom

The main advantages of use CustomUserType are:

  1. you can store into DB an integer (that is more smaller) instead of a string that represents the enum.
  2. Delegate the parsing to hibernate during storing and retrieving of object.
  3. You can use the enum directly into the query (like you are trying to do)

尝试使用<>代替!=如下所示:

  "from TypeActionCommercialeImpl where type <> :type1"

i resolve my pb i have a class public class EnumUserType<E extends Enum<E>> implements UserType and i implement this method:

@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());  
    }  

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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