-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
.../hibernate/orm/test/mapping/converted/converter/map/MapElementBaseTypeConversionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |