We're updating the issue view to help you get more done. 

Unable to filter criteria by attribute with @Convert

Description

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @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:

  1. isMember

    1 q.where(cb.isMember("CAI", c.<Set<String>>get("teams")));

    //java.lang.IllegalArgumentException: unknown collection expression type [org.hibernate.jpa.criteria.path.SingularAttributePath]

  1. equalToSet

    1 2 3 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

  2. equalToString

    1 q.where(cb.equal(c.get("teams"), "CAI"));

    // java.lang.IllegalArgumentException: Parameter value [CAI] did not match expected type [java.util.Set (n/a)]

Environment

None

Status

Assignee

Unassigned

Reporter

Federico Gaule

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.2.2

Priority

Major