diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/convert/ConvertedAttributeConcatTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/convert/ConvertedAttributeConcatTest.java new file mode 100644 index 0000000000..954b0fd977 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/convert/ConvertedAttributeConcatTest.java @@ -0,0 +1,104 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.jpa.criteria.convert; + +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Convert; +import jakarta.persistence.Converter; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * @author Marco Belladelli + */ +@TestForIssue(jiraKey = "HHH-15823") +@DomainModel(annotatedClasses = ConvertedAttributeConcatTest.Post.class) +public class ConvertedAttributeConcatTest { + @Test + @ServiceRegistry(settings = { + @Setting(name = AvailableSettings.CRITERIA_VALUE_HANDLING_MODE, value = "inline") + }) + @SessionFactory + public void testConvertedAttributeConcatInline(SessionFactoryScope scope) { + scope.inTransaction( this::executeTestQuery ); + } + + @Test + @ServiceRegistry(settings = { + @Setting(name = AvailableSettings.CRITERIA_VALUE_HANDLING_MODE, value = "bind") + }) + @SessionFactory + public void testConvertedAttributeConcatBind(SessionFactoryScope scope) { + scope.inTransaction( this::executeTestQuery ); + } + + private void executeTestQuery(Session session) { + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery( Post.class ); + Root root = query.from( Post.class ); + query.select( root ).where( cb.like( cb.concat( ",", root.get( "tags" ) ), "%foo%" ) ); + assertNotNull( session.createQuery( query ).getResultList() ); + } + + @Entity(name = "Post") + public static class Post { + @Id + private Long id; + + @Convert(converter = StringSetConverter.class) + private Set tags; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getTags() { + return tags; + } + + public void setTags(Set tags) { + this.tags = tags; + } + } + + @Converter + public static class StringSetConverter implements AttributeConverter, String> { + @Override + public String convertToDatabaseColumn(Set attribute) { + return attribute == null ? null : String.join( ",", attribute ); + } + + @Override + public Set convertToEntityAttribute(String dbData) { + return dbData == null ? null : new LinkedHashSet<>( Arrays.asList( dbData.split( "," ) ) ); + } + } +}