Skip to content

Commit

Permalink
HHH-15733 Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel authored and beikov committed Feb 6, 2023
1 parent 0dfba1e commit dfedde8
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit dfedde8

Please sign in to comment.