Unable to filter criteria by attribute with @Convert
Description
Attachments
2
- 21 Sep 2016, 03:13 PM
- 21 Sep 2016, 12:32 PM
Activity
Show:
Lucas Wojciechow May 19, 2021 at 10:15 PM
I would also like an update on this
Razvan Savu August 3, 2019 at 6:19 AM
Any update on this?
Federico Gaule Palombarani September 27, 2016 at 12:04 PM
Yes, you are right
Former user September 27, 2016 at 5:44 AM
@Federico Gaule Palombarani, I see 3 zips:
hibernate_converter_set_5.2.2.zip
hibernate_converter_set_5.22_withUserType.zip
hibernate_converter_set.zip
Does hibernate_converter_set_5.2.2.zip supersede hibernate_converter_set.zip?
Can I delete everything except hibernate_converter_set_5.2.2.zip and hibernate_converter_set_5.22_withUserType.zip?
Vlad Mihalcea September 21, 2016 at 3:40 PM
I don't have the rights to manage them
Details
Details
Assignee
Unassigned
UnassignedReporter
Federico Gaule Palombarani
Federico Gaule PalombaraniLabels
Components
Affects versions
Priority
Created July 28, 2015 at 11:52 AM
Updated May 19, 2021 at 10:15 PM
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)]