java.lang.ClassCastException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • When I set a query paramter on a TypedQuery that is a primitive array (in my case byte[]) there will be a ClassCastException inside the method org.hibernate.ejb.AbstractQueryImpl#registerParameterBinding because the method tries to cast byte[] to Object[]: {noformat} ... else if (value.getClass().isArray()) { final Object[] array = (Object[]) value; ... {noformat} *Produces:* {noformat} java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object; at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:348) at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:359) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) {noformat} *Reproduction is easy:* {noformat} em.createQuery("SELECT e.stringValue FROM entity e WHERE e.guid = :guid", String.class).setParameter("guid", new byte[16]); {noformat} having entity defined as: {noformat} @Entity(name = "entity") public class Entity { @Column(length=16) byte[] guid; @Column String stringValue; } {noformat} *Solution:* The solution to the problem is quite simple. By checking list types only if the direct type match doesn't return true, the system doesn't have such an issue. The following patch fixes the bug: Patch for the method inside org.hibernate.ejb.AbstractQueryImpl.java[274]: {noformat} protected void registerParameterBinding(Parameter parameter, Object value) { final Class<?> targetType = parameter.getParameterType(); if ( value != null && targetType != null ) { if (!targetType.isInstance(value)) { if ( Collection.class.isInstance( value ) ) { final Collection collection = (Collection) value; // validate the elements... for ( Object element : collection ) { if ( ! targetType.isInstance( element ) ) { throw new IllegalArgumentException( "Parameter value [" + element + "] was not matching type [" + targetType.getName() + "]" ); } } } else if ( value.getClass().isArray() ) { final Object[] array = (Object[]) value; for ( Object element : array ) { if ( ! targetType.isInstance( element ) ) { throw new IllegalArgumentException( "Parameter value [" + element + "] was not matching type [" + targetType.getName() + "]" ); } } } else { throw new IllegalArgumentException( "Parameter value [" + value + "] was not matching type [" + targetType.getName() + "]" ); } } } if ( parameterBindings == null ) { parameterBindings = new HashMap(); } parameterBindings.put( parameter, value ); } {noformat}
    via by Jürgen Kellerer,
  • When I set a query paramter on a TypedQuery that is a primitive array (in my case byte[]) there will be a ClassCastException inside the method org.hibernate.ejb.AbstractQueryImpl#registerParameterBinding because the method tries to cast byte[] to Object[]: {noformat} ... else if (value.getClass().isArray()) { final Object[] array = (Object[]) value; ... {noformat} *Produces:* {noformat} java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object; at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:348) at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:359) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) {noformat} *Reproduction is easy:* {noformat} em.createQuery("SELECT e.stringValue FROM entity e WHERE e.guid = :guid", String.class).setParameter("guid", new byte[16]); {noformat} having entity defined as: {noformat} @Entity(name = "entity") public class Entity { @Column(length=16) byte[] guid; @Column String stringValue; } {noformat} *Solution:* The solution to the problem is quite simple. By checking list types only if the direct type match doesn't return true, the system doesn't have such an issue. The following patch fixes the bug: Patch for the method inside org.hibernate.ejb.AbstractQueryImpl.java[274]: {noformat} protected void registerParameterBinding(Parameter parameter, Object value) { final Class<?> targetType = parameter.getParameterType(); if ( value != null && targetType != null ) { if (!targetType.isInstance(value)) { if ( Collection.class.isInstance( value ) ) { final Collection collection = (Collection) value; // validate the elements... for ( Object element : collection ) { if ( ! targetType.isInstance( element ) ) { throw new IllegalArgumentException( "Parameter value [" + element + "] was not matching type [" + targetType.getName() + "]" ); } } } else if ( value.getClass().isArray() ) { final Object[] array = (Object[]) value; for ( Object element : array ) { if ( ! targetType.isInstance( element ) ) { throw new IllegalArgumentException( "Parameter value [" + element + "] was not matching type [" + targetType.getName() + "]" ); } } } else { throw new IllegalArgumentException( "Parameter value [" + value + "] was not matching type [" + targetType.getName() + "]" ); } } } if ( parameterBindings == null ) { parameterBindings = new HashMap(); } parameterBindings.put( parameter, value ); } {noformat}
    via by Jürgen Kellerer,
  • Update byte array JPA postgres
    via by Michal Horowic,
    • java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object; at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:349) at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:364) at org.hibernate.ejb.criteria.CriteriaQueryCompiler$1$1.bind(CriteriaQueryCompiler.java:194) at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:247) at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:441) at ru.petrsu.nest.oqlite.test.TestParser.main(TestParser.java:92)
    No Bugmate found.