2013-05-05 75 views
0

我們使用自定義的Hibernate UserType在一行中存儲一組字符串。當UserType表示單行對象集時。如何用類似條件查詢Hibernate自定義的UserType?

當試圖查詢此組與標準,使用JPA CriteriaBuilder休眠拋出拋出:IllegalArgumentException

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

是否有解決方法嗎?

這裏是我們使用的是用戶類型:

public class SetStringType implements UserType, LiteralType<Set<String>> { 

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<String> resultValues = new HashSet<String>(); 

    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<String>) 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<String>) value).clone(); 

    return new HashSet<String>((Collection<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 original; 
} 

@Override 
public String objectToSQLString(Set<String> value, Dialect dialect) throws Exception { 
    return StringUtils.collectionToDelimitedString(value, SEPARATOR); 
} 
} 

回答

0

從春天的javadoc

collectionToDelimitedString 

public static String collectionToDelimitedString(Collection coll, 
              String delim) 

Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations. 

Parameters: 
    coll - the Collection to display 
    delim - the delimiter to use (probably a ",") 
Returns: 
    the delimited String 

所以你的線是不正確的,因爲你設置一個字符串不是一個集合,因爲它的預期

st.setString(index, StringUtils.collectionToDelimitedString((Collection<String>) value, SEPARATOR)); 
相關問題