HHH-15823 Add test for issue

This commit is contained in:
Marco Belladelli 2022-12-14 12:24:54 +01:00 committed by Christian Beikov
parent 919e322600
commit ba1feef1f8
1 changed files with 104 additions and 0 deletions

View File

@ -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<Post> query = cb.createQuery( Post.class );
Root<Post> 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<String> tags;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<String> getTags() {
return tags;
}
public void setTags(Set<String> tags) {
this.tags = tags;
}
}
@Converter
public static class StringSetConverter implements AttributeConverter<Set<String>, String> {
@Override
public String convertToDatabaseColumn(Set<String> attribute) {
return attribute == null ? null : String.join( ",", attribute );
}
@Override
public Set<String> convertToEntityAttribute(String dbData) {
return dbData == null ? null : new LinkedHashSet<>( Arrays.asList( dbData.split( "," ) ) );
}
}
}