Weird Behavior for byte[] array in Hibernate Objects #1431

Closed
voor opened this Issue Jul 10, 2015 · 3 comments

Projects

None yet

3 participants

@voor
voor commented Jul 10, 2015

Attempting to utilize a class that has a field mapping byte[] to Blob. Getting the following error:

[B cannot be cast to [Ljava.lang.Object;
! java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
! at com.querydsl.jpa.hibernate.HibernateUtil.setValue(HibernateUtil.java:71) ~[querydsl-jpa-4.0.2.jar:na]
! at com.querydsl.jpa.hibernate.HibernateUtil.setConstants(HibernateUtil.java:63) ~[querydsl-jpa-4.0.2.jar:na]
! at com.querydsl.jpa.hibernate.AbstractHibernateQuery.createQuery(AbstractHibernateQuery.java:105) ~[querydsl-jpa-4.0.2.jar:na]
! at com.querydsl.jpa.hibernate.AbstractHibernateQuery.createQuery(AbstractHibernateQuery.java:97) ~[querydsl-jpa-4.0.2.jar:na]
! at com.querydsl.jpa.hibernate.AbstractHibernateQuery.fetch(AbstractHibernateQuery.java:174) ~[querydsl-jpa-4.0.2.jar:na]
@voor
voor commented Jul 10, 2015

Looks like it's because a byte array is considered an array.

It's attempting to cast the array to an Object array, which you can't do with a byte array.

    private static void setValue(Query query, String key, Object val) {
        if (val instanceof Collection<?>) {
            query.setParameterList(key, (Collection<?>) val);
        } else if (val.getClass().isArray()) {
            query.setParameterList(key, (Object[]) val);
        } else if (TYPES.containsKey(val.getClass())) {
            query.setParameter(key, val, TYPES.get(val.getClass()));
        } else {
            query.setParameter(key, val);
        }
    }

Would this possibly fix it? (Or something similar)

    private static void setValue(Query query, String key, Object val) {
        if (val instanceof Collection<?>) {
            query.setParameterList(key, (Collection<?>) val);
        } else if (val.getClass().equals(byte[].class)) {
            ...
        } else if (val.getClass().isArray()) {
            query.setParameterList(key, (Object[]) val);
        } else if (TYPES.containsKey(val.getClass())) {
            query.setParameter(key, val, TYPES.get(val.getClass()));
        } else {
            query.setParameter(key, val);
        }
    }
@Shredder121
Member

Sorry for not commenting earlier.
As for your PR, I think that it can be solved using the TYPES map (if we reorder the statements, there are HibernateTypes for byte arrays to blobs, so it can be handled by Hibernate).

I do need to ask whether it was @timowest's intention to make it customizable.

@voor
voor commented Jul 11, 2015

No worries, this is just a showstopper for me, and I wanted to do whatever I could to help it get resolved.

@timowest timowest added the progress label Jul 17, 2015
@johnktims johnktims closed this in #1440 Jul 30, 2015
@timowest timowest added this to the 4.0.3 milestone Jul 31, 2015
@timowest timowest removed the progress label Jul 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment