当UserType表示单行中的对象集时。 如何使用类似标准查询Hibernate自定义UserType?

我们使用自定义Hibernate UserType将一组字符串存储在一行中。

当尝试使用类似标准查询此集合时,使用JPA CriteriaBuilder Hibernate会抛出IllegalArgumentException

Parameter value String did not match expected type java.util.Set

这有解决方法吗?

这是我们正在使用的UserType:

 public class SetStringType implements UserType, LiteralType<Set> { final private static String SEPARATOR = "|"; final private static String SEPARATOR_REGEXP = "\\|"; @Override public int[] sqlTypes() { return new int[] { Types.VARCHAR }; } @Override public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { HashSet resultValues = new HashSet(); String value = rs.getString(names[0]); if (value == null) return resultValues; String[] values = value.split(SEPARATOR_REGEXP); resultValues.addAll(Arrays.asList(values)); return resultValues; } @Override @SuppressWarnings("unchecked") public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { st.setString(index, StringUtils.collectionToDelimitedString((Collection) value, SEPARATOR)); } @Override @SuppressWarnings("rawtypes") public Class returnedClass() { return Set.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 @SuppressWarnings("unchecked") public Object deepCopy(Object value) throws HibernateException { if (value == null) return null; if (value instanceof HashSet) return ((HashSet) value).clone(); return new HashSet((Collection) 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 original; } @Override public String objectToSQLString(Set value, Dialect dialect) throws Exception { return StringUtils.collectionToDelimitedString(value, SEPARATOR); } } 

从springjavadoc

  collectionToDelimitedString public static String collectionToDelimitedString(Collection coll, String delim) Convenience method to return a Collection as a delimited (eg CSV) String. Eg useful for toString() implementations. Parameters: coll - the Collection to display delim - the delimiter to use (probably a ",") Returns: the delimited String 

因此,您的行不正确,因为您设置的字符串不是预期的Set

 st.setString(index, StringUtils.collectionToDelimitedString((Collection) value, SEPARATOR));