I have an entity and a property in it that has AttributeConverter that converts a Set of enum to string and vice versa as followed:
@Entity
public class Foo {
@Column
@Convert(converter = MyConverter.class)
private Set<MyEnum> enumSet;
...
}
And my converter is not any different than any of the other converters seen everywhere:
@Converter
public class MyConverter implements AttributeConverter<Set<MyEnum>, String> {
@Override
public String convertToDatabaseColumn(Set<MyEnum> set) {
if (set == null)
return null;
StringBuilder builder = new StringBuilder();
Iterator<MyEnum> it = set.iterator();
while (it.hasNext()) {
builder.append(it.next().name());
if (it.hasNext()) {
builder.append(Constants.SEPARATOR);
}
return builder.toString();
}
@Override
public Set<MyEnum> convertToEntityAttribute(String dbData) {
if (dbData == null || dbData.isEmpty())
return null;
String[] oarts = dbData.split(Constants.SEPARATOR);
...
}
Now, when I try to save my data to my database using Hibernate session, and also from the frontend, everything works fine.
In my database, the result of my enum is converted correctly:
property1 | enumset | ……. |
---|---|---|
…. | ENUM_!, ENUM2, … | ….. |
Now, however, I have to add a validation that checks if the data with the given enumSet already exists in the database before saving the new data in my function:
public List<MyData> findMyDataBy(Set<MyEnum> enumSet) {
try (var session = sessionFactory.openSession()) {
var criteriaBuilder = session.getCriteriaBuilder();
var query = criteriaBuilder.createQuery(MyData.class);
var root = query.from(MyData.class);
query.select(root).where(criteriaBuilder.equal(root.get("enumSet"), enumSet)));
var result = session.createQuery(query).getResultList();
return result;
} catch (Exception e) {
...
}
But now, when I try to save it, I always receive the following errors:
Caused by: javax.persistence.PersistenceException: Error attempting to apply AttributeConverter
...
Caused by: java.lang.ClassCastException: class myfoo.bar.baz.enums.MyEnum cannot be cast to class java.util.Set
I also tried to search for the result using a String combining all the enum values and using the criteriaBuilder.like()
method but I got the error –>
Caused by: java.lang.ClassCastExceptioon: class String cannot be cast to class java.util.Set
Since I can retrieve the data in my database using raw SQL as follows:
SELECT * FROM my_table WHERE enumset LIKE '%ENUM_1,ENUM_2,...%';
or with
SELECT * FROM my_table WHERE enumset = 'ENUM_!,ENUM_2,...';
I would also want to know if there was a way to retrieve the data with criteriaBuilder as a String instead of a Set, for example using a Tuple and then a method that would convert it, since I do not know what I should change in my converter as it works perfectly to save the data and I would like to keep it that way and find quick solutions.
I expected to receive the list of data that has the enumSet that the user typed in the form and received an AttributeConverter error instead, so I’d want to get the data using the criteriaBuilder but taking the path (root.get(“enumSet”)) as a String if possible or otherwise another solution that keeps the Set but makes it work.
Thanks a lot.
I found a solution. The first solution was to use native SQL obviously, but I needed to use the Predicates in my case:
var sql = "SELECT * FROM my_data WHERE enum_set = '" + enumSetString + "'";
var result = session.createNativeSQL(sql, MyData.class);
.....
and another solution that works as well, and which uses the Predicates, is to use a database specific function like “TEXT” for example for PostgreSQL to compare the String directly in the database:
var function = criteriaBuilder.function("TEXT", String.class, root.get("enumSet"));
var pred = criteriaBuilder.equal(function, criteriaBuilder.literal(enumSetString));
I hope it helps 🙂