Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - Can't execute query with a UserType in where clause

I have a Hibernate UserType defined to transform data before it goes into our database and then un-transform it when it's read back from the db. This works well when I insert a row or get rows using the row's ID or some other way to query for the row. However, when I try to use a query to find a record, the parameter binding seems to fail:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [thisIsTheSearchString] did not match expected type [com.xxx.MyUserType (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [thisIsTheSearchString] did not match expected type [com.xxx.MyUserType (n/a)]

I tried implementing LiteralType and the objectToSQLString method but it doesn't look like this method is ever called.

As a simplified example:

public class MyUserType implements UserType, LiteralType {

    @Override
    public int[] sqlTypes() {
        return new int[] {
                Types.VARCHAR
        };
    }

    @Override
    public Class returnedClass() {
        return MyUserType.class;
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        return ObjectUtils.equals(x, y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        assert (x != null);
        return x.hashCode();
    }

    @Override
    public Object nullSafeGet(
            ResultSet rs, 
            String[] names, 
            SessionImplementor session, 
            Object owner) 
                    throws HibernateException, SQLException
    {
        assert names.length == 1;
        return untransform( rs.getString( names[0] ); );
    }

    String transform(String untransformed) {
        //...
    }

    String untransform(String transformed) {
        //...
    }

    @Override
    public void nullSafeSet(
            PreparedStatement st, 
            Object value, 
            int index,
            SessionImplementor session)
                    throws HibernateException, SQLException 
    {
        if ( value == null ) {
            st.setNull(index, Types.VARCHAR);
        } else {
            final String untransformed = (String)value;

            return transform(untransformed);
        }
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        if ( value == null ) {
            return null;
        }
        return (String)value;
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) deepCopy(value);
    }

    @Override
    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return deepCopy(cached);
    }

    @Override
    public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
        return deepCopy(original);
    }

    // THIS NEVER GETS CALLED
    @Override
    public String objectToSQLString(Object value, Dialect dialect)
            throws Exception 
    {
        if ( value == null ) {
            return null;
        }

        String transformed = transform((String)value);

        StringType stringType = new StringType();
        String sqlString = stringType.objectToSQLString(transformed, dialect);

        return sqlString;
    }
}

The entity looks like:

@Entity
@Table(name = "blah_blah")
@TypeDefs(value = { @TypeDef(name = "transformedText", typeClass = MyUserType.class)})
public class BlahBlah implements Serializable, Persistable<Long> {

    //...

    @Column(name = "transformed")
    @Type(type = "transformedText")
    String transformed;

    //...
}

My query:

@Query(value = 
        "select b " +
        "from BlahBlah b " +
        "where b.transformed = ?1 ")
public List<BlahBlah> findTransformed(String text);
like image 895
Chris Williams Avatar asked Mar 30 '15 20:03

Chris Williams


2 Answers

I think you need to change the returned class:

@Override
public Class returnedClass() {
    return MyUserType.class;
}

should be:

@Override
public Class returnedClass() {
    return String.class;
}

In the docs (https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/usertype/UserType.html#returnedClass()):

returnedClass

Class returnedClass()

The class returned by nullSafeGet(). Returns: Class

and your nullSafeGet appears to be returning a String.

like image 190
wholevinski Avatar answered Nov 07 '22 04:11

wholevinski


With Spring Data you can implement a custom query implementation and you can unwrap the EntityManager to a Hibernate Session, and then supply the Custom Type you created to your query:

@PersistenceContext
private EntityManager entityManager;

public List<BlahBlah> findTransformed(String text) {
    Session session = entityManager.unwrap(Session.class);

    TypeHelper typeHelper = session.getSessionFactory().getTypeHelper();

    List<BlahBlah> result = (List<BlahBlah>) session.createQuery(
        "select b " +
        "from BlahBlah b " +
        "where b.transformed = :transformed ")
    .setParameter("transformed", text, typeHelper.custom(MyUserType.class))
    .list();
}  
like image 31
Vlad Mihalcea Avatar answered Nov 07 '22 05:11

Vlad Mihalcea