-
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
109 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
...org/hibernate/orm/test/mapping/converted/converter/ConvertedEmbeddableCollectionTest.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,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 ); | ||
} | ||
} | ||
} |