HHH-15733 Add test for issue

This commit is contained in:
Marco Belladelli 2022-11-24 16:48:42 +01:00 committed by Christian Beikov
parent 07529c309d
commit b1a16f419c
1 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,97 @@
/*
* 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.mapping.converted.converter.map;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Convert;
import jakarta.persistence.Converter;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Marco Belladelli
*/
@DomainModel(annotatedClasses = {
MapElementBaseTypeConversionTest.Customer.class
})
@SessionFactory
@JiraKey("HHH-15733")
public class MapElementBaseTypeConversionTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
Customer customer = new Customer();
customer.getColors().put( "eyes", "blue" );
session.persist( customer );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from Customer" ).executeUpdate() );
}
@Test
public void testBasicElementCollectionConversion(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
Customer customer = session.find( Customer.class, 1 );
assertEquals( 1, customer.getColors().size() );
assertEquals( "blue", customer.getColors().get( "eyes" ) );
} );
}
@Entity(name = "Customer")
public static class Customer {
@Id
@GeneratedValue
private Integer id;
@ElementCollection(fetch = FetchType.EAGER)
@Convert(converter = MyStringConverter.class)
private final Map<String, String> colors = new HashMap<>();
public Customer() {
}
public Customer(Integer id) {
this.id = id;
}
public Map<String, String> getColors() {
return colors;
}
}
@Converter
public static class MyStringConverter implements AttributeConverter<String, String> {
@Override
public String convertToDatabaseColumn(String attribute) {
return new StringBuilder( attribute ).reverse().toString();
}
@Override
public String convertToEntityAttribute(String dbData) {
return new StringBuilder( dbData ).reverse().toString();
}
}
}