-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
131 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
...e/orm/test/annotations/embeddables/generics/CompositeUserTypeInGenericSuperclassTest.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,131 @@ | ||
/* | ||
* 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.annotations.embeddables.generics; | ||
|
||
import org.hibernate.annotations.CompositeType; | ||
import org.hibernate.orm.test.mapping.embeddable.strategy.usertype.embedded.Name; | ||
import org.hibernate.orm.test.mapping.embeddable.strategy.usertype.embedded.NameCompositeUserType; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.Jira; | ||
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.Embeddable; | ||
import jakarta.persistence.Embedded; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.MappedSuperclass; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Marco Belladelli | ||
*/ | ||
@DomainModel( annotatedClasses = { | ||
CompositeUserTypeInGenericSuperclassTest.GenericSuperclass.class, | ||
CompositeUserTypeInGenericSuperclassTest.TestEmbeddable.class, | ||
CompositeUserTypeInGenericSuperclassTest.TestEntity.class, | ||
} ) | ||
@SessionFactory | ||
@Jira( "https://hibernate.atlassian.net/browse/HHH-17916" ) | ||
public class CompositeUserTypeInGenericSuperclassTest { | ||
@Test | ||
public void testFind(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final TestEntity result = session.find( TestEntity.class, 1L ); | ||
assertThat( result.getGenericEmbedded().getTestProp() ).isEqualTo( 1 ); | ||
assertThat( result.getName().firstName() ).isEqualTo( "Marco" ); | ||
assertThat( result.getName().lastName() ).isEqualTo( "Belladelli" ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testQuery(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final TestEntity result = session.createQuery( | ||
"from TestEntity where genericEmbedded.testProp = 2", | ||
TestEntity.class | ||
).getSingleResult(); | ||
assertThat( result.getGenericEmbedded().getTestProp() ).isEqualTo( 2 ); | ||
assertThat( result.getName().firstName() ).isEqualTo( "Andrea" ); | ||
assertThat( result.getName().lastName() ).isEqualTo( "Boriero" ); | ||
} ); | ||
} | ||
|
||
@BeforeAll | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
session.persist( new TestEntity( 1L, new Name( "Marco", "Belladelli" ), new TestEmbeddable( 1 ) ) ); | ||
session.persist( new TestEntity( 2L, new Name( "Andrea", "Boriero" ), new TestEmbeddable( 2 ) ) ); | ||
} ); | ||
} | ||
|
||
@AfterAll | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() ); | ||
} | ||
|
||
@MappedSuperclass | ||
static class GenericSuperclass<T> { | ||
@Embedded | ||
@CompositeType( NameCompositeUserType.class ) | ||
private Name name; | ||
|
||
@Embedded | ||
T genericEmbedded; | ||
|
||
public GenericSuperclass() { | ||
} | ||
|
||
public GenericSuperclass(Name name, T genericEmbedded) { | ||
this.name = name; | ||
this.genericEmbedded = genericEmbedded; | ||
} | ||
|
||
public Name getName() { | ||
return name; | ||
} | ||
|
||
public T getGenericEmbedded() { | ||
return genericEmbedded; | ||
} | ||
} | ||
|
||
@Embeddable | ||
static class TestEmbeddable { | ||
private Integer testProp; | ||
|
||
public TestEmbeddable() { | ||
} | ||
|
||
public TestEmbeddable(Integer testProp) { | ||
this.testProp = testProp; | ||
} | ||
|
||
public Integer getTestProp() { | ||
return testProp; | ||
} | ||
} | ||
|
||
@Entity( name = "TestEntity" ) | ||
static class TestEntity extends GenericSuperclass<TestEmbeddable> { | ||
@Id | ||
private Long id; | ||
|
||
public TestEntity() { | ||
} | ||
|
||
public TestEntity(Long id, Name name, TestEmbeddable genericEmbedded) { | ||
super( name, genericEmbedded ); | ||
this.id = id; | ||
} | ||
} | ||
} |