0

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();
}
13
  • Hey guy, maybe you have to use setProperties method. Try ! Commented Jan 25, 2016 at 21:35
  • The query is good. The problem states that parameter 1 is missing. Where is TypeActionCommercialeEnum.NIP? It is like it is not in the classpath. Can you try "from TypeActionCommercialeImpl where type != TypeActionCommercialeEnum.NIP" (and remove the setParameter line) to see what error you get Commented Jan 25, 2016 at 21:58
  • @IanMc error: org.hibernate.QueryException: Unable to resolve path [TypeActionCommercialeEnum.NIP], unexpected token [TypeActionCommercialeEnum] [from com.metier.impl.TypeActionCommercialeImpl where type != TypeActionCommercialeEnum.NIP] Commented Jan 25, 2016 at 22:02
  • Better. Now you can focus on the real problem. Commented Jan 25, 2016 at 22:04
  • How do you define type in the Entity class? Commented Jan 25, 2016 at 22:16

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

10 Comments

What type is the field "type"?
type character varying(255)
The problem is that the keyword "type" is private keyword. Can you try to change the field name for a test please?
it's work under Hibernate 3 here i'm under Hibernate 5
it's very strange problem... try to use setString() and paste the stack trace
|
0

Try to use <> instead of != like this:

  "from TypeActionCommercialeImpl where type <> :type1"

2 Comments

Use setProperties method Instead setParameter. And change != to <>
The method getResultList() is undefined for the type Query
0

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

} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.