java - When UserType represents Set of Objects in single line. How to query Hibernate custom UserType with like criteria? -


we using custom hibernate usertype store set of strings in single line.

when trying query set like criteria, using jpa criteriabuilder hibernate throws illegalargumentexception

parameter value string did not match expected type java.util.set

is there workaround this?

here usertype using:

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); } } 

from spring javadoc

 collectiontodelimitedstring  public static string collectiontodelimitedstring(collection coll,                                              string delim)   convenience method return collection delimited (e.g. csv) string. e.g. useful tostring() implementations.  parameters:     coll - collection display     delim - delimiter use (probably ",")  returns:     delimited string 

so line not correct because setting string not set it's expected

st.setstring(index, stringutils.collectiontodelimitedstring((collection<string>) value, separator)); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -