when compiling a fragment below crash

List <shop> products = null; Criteria criteria= session.createCriteria(shop.class); criteria.add(Restrictions.eq("id", 1)); products = criteria.list(); 

crash itself:

 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36) at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275) at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1968) at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1939) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1874) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1835) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815) at org.hibernate.loader.Loader.doQuery(Loader.java:899) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) at org.hibernate.loader.Loader.doList(Loader.java:2522) at org.hibernate.loader.Loader.doList(Loader.java:2508) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338) at org.hibernate.loader.Loader.list(Loader.java:2333) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 

but the problem is present only if the id field, because if there is:

 criteria.add(Restrictions.eq("name", "clara")); 

then everything is fine. The entry with the name Clara is present as well as the entry with id equal to 1 How to treat?

PS Initially, Karsh was impressed by the fact that in the id code it was indicated as a long if you replace it with an int then everything is fine, not counting the work with arrays, the option with an array

 int []mas= {1, 2, 3, 4, 5, 6}; Criteria criteria= session.createCriteria(shop.class); criteria.add(Restrictions.eq("id", mas)); products = criteria.list(); 

crash:

 java.lang.ClassCastException: [I cannot be cast to java.lang.Integer at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36) at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:57) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275) at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1968) at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1939) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1874) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1835) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815) at org.hibernate.loader.Loader.doQuery(Loader.java:899) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) at org.hibernate.loader.Loader.doList(Loader.java:2522) at org.hibernate.loader.Loader.doList(Loader.java:2508) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338) at org.hibernate.loader.Loader.list(Loader.java:2333) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 
  • one
    Did you try to read the error message? Right on the first line solution to your problem. Correct 1 for 1L . - enzo
  • @enzo in my head is now "cheั‘ั‘ั‘ั‘ั‘ั‘ั‘ ...". But seriously, if that is how to treat it? for if I have not a number but an array of numbers? Yes, and I can also give a number by variable - Clara Oswald
  • @ClaraOswald Even if it is, then nothing prevents you from explicitly variable: criteria.add(Restrictions.eq("id", (Long)parm)); - StateItPrimitive

1 answer 1

The reason is that you pass a variable of the wrong type to the expression Restrictions.eq("id", 1) . The variable id in your Entity , as follows from the exception text, is of type Long

 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long 

In Java, a literal written with a prime number is Integer in the application source. Read the Literals section here , or take any book with the basics of the language, such as Horstman.

Integer cannot be cast in Long . In such cases, you must use an explicit conversion. For example, Long.valueOf(int) or new Long(int) .

To pass a variable of the correct type to your example, use the literal 1L .

But seriously, if that is how to treat it? for if I have not a number but an array of numbers? Yes, and I can also give a number by variable

Since the eq() method takes as a value the ( value ) Object treatment, no matter how cool it will be, to make sure that the type of the variable matches the one you declared in the Entity , and not shove everything there.

To get an object by id there is a simpler method :

 MyEntity myEntity = (MyEntity) session.get(MyEntity.class, id) 
  • and what with option where the array of values โ€‹โ€‹is used? there it is already fixed in the code that id has the value int and not long - Clara Oswald
  • The eq() method in this case does not resemble. Use Restrictions.in("id", ids) . First convert the array to List<Long> . - enzo