Skip to content

Commit

Permalink
HHH-17071 Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel authored and beikov committed Aug 28, 2023
1 parent fd961ce commit 57e9efa
Showing 1 changed file with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}

0 comments on commit 57e9efa

Please sign in to comment.