java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;

Hibernate JIRA | Jürgen Kellerer | 7 years ago
tip
Do you know that we can give you better hits? Get more relevant results from Samebug’s stack trace search.
  1. 0

    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}

    Hibernate JIRA | 7 years ago | Jürgen Kellerer
    java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
  2. 0

    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}

    Hibernate JIRA | 7 years ago | Jürgen Kellerer
    java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
  3. 0

    Update byte array JPA postgres

    Coderanch | 5 years ago | Michal Horowic
    java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.ClassCastException

      [B cannot be cast to [Ljava.lang.Object;

      at org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding()
    2. Hibernate EJB
      QueryImpl.setParameter
      1. org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:348)
      2. org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:359)
      2 frames
    3. Java RT
      DelegatingMethodAccessorImpl.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      3 frames
    4. Spring ORM
      SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke
      1. org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310)
      1 frame