Trying to filter a query by an attribute with @Convert is failling. I have tried to find a workaround with no success.
The case is simple (attached), there is a class with an attribute of type Set. This attribute is wanted to be saved as a String (VARCHAR) in database.
@Entity
public class MyEntity {
private Long id;
private Set<String> teams;
public MyEntity() {
}
public MyEntity(Set<String> teams) {
this.teams = teams;
}
@Id
@GeneratedValue
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Convert(converter = TeamsConverter.class)
@Column(name = "MY_ENTITY_FIELDS")
public Set<String> getTeams() {
return teams;
}
public void setTeams(Set<String> teams) {
this.teams = teams;
}
}
Converter translates from Set to String and viceversa
@Converter
public class TeamsConverter implements AttributeConverter<Set, String> {
public String convertToDatabaseColumn(Set set) {
return set == null ? null : String.join("|", set);
}
public Set convertToEntityAttribute(String s) {
HashSet set = new HashSet();
if (s != null) {
for (String splitted : s.split("|")) {
set.add(splitted);
}
}
return set;
}
}
I have tried 3 ways of doing this leading me to open this issue:
-
isMember
q.where(cb.isMember("CAI", c.<Set<String>>get("teams")));
//java.lang.IllegalArgumentException: unknown collection expression type [org.hibernate.jpa.criteria.path.SingularAttributePath]
-
equalToSet
Set<String> a = new HashSet<String>();
a.add("CAI");
q.where(cb.equal(c.get("teams"), a));
// java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Set
-
equalToString
q.where(cb.equal(c.get("teams"), "CAI"));
// java.lang.IllegalArgumentException: Parameter value [CAI] did not match expected type [java.util.Set (n/a)]
|