HHH-17071 Add test for issue

This commit is contained in:
Marco Belladelli 2023-08-22 15:18:10 +02:00 committed by Christian Beikov
parent fd961cebb1
commit 57e9efa4e2
1 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,109 @@
/*
* 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;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.testing.orm.junit.DomainModel;
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.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Marco Belladelli
*/
@DomainModel( annotatedClasses = {
ConvertedEmbeddableCollectionTest.TestEmbeddable.class,
ConvertedEmbeddableCollectionTest.TestEntity.class,
} )
@SessionFactory
public class ConvertedEmbeddableCollectionTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final TestEntity entity = new TestEntity( 1L );
entity.getEmbeddables().add( new TestEmbeddable( "test" ) );
session.persist( entity );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() );
}
@Test
public void testMapping(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat(
session.find( TestEntity.class, 1L )
.getEmbeddables()
.stream()
.map( TestEmbeddable::getData )
).containsExactly( "test" ) );
}
@Embeddable
public static class TestEmbeddable {
private String data;
public TestEmbeddable() {
}
public TestEmbeddable(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
@Entity( name = "TestEntity" )
public static class TestEntity {
@Id
private Long id;
@Convert( converter = EmbeddableConverter.class )
private Set<TestEmbeddable> embeddables = new HashSet<>();
public TestEntity() {
}
public TestEntity(Long id) {
this.id = id;
}
public Set<TestEmbeddable> getEmbeddables() {
return embeddables;
}
}
@Converter
public static class EmbeddableConverter implements AttributeConverter<TestEmbeddable, String> {
@Override
public String convertToDatabaseColumn(TestEmbeddable attribute) {
return attribute.getData();
}
@Override
public TestEmbeddable convertToEntityAttribute(String dbData) {
return new TestEmbeddable( dbData );
}
}
}