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
2 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
...rnate/orm/test/annotations/embeddables/EmbeddableAndMappedSuperClassWithGenericsTest.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,145 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
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.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.MappedSuperclass; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
EmbeddableAndMappedSuperClassWithGenericsTest.PopularBook.class, | ||
EmbeddableAndMappedSuperClassWithGenericsTest.RareBook.class | ||
} | ||
) | ||
@SessionFactory | ||
@TestForIssue(jiraKey = "HHH-15552") | ||
public class EmbeddableAndMappedSuperClassWithGenericsTest { | ||
|
||
private final static long POPULAR_BOOK_ID = 1l; | ||
private final static long RARE_BOOK_ID = 2l; | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
Edition popularEdition = new Edition( "Popular" ); | ||
PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition, "POP" ); | ||
|
||
Edition rareEdition = new Edition( "Rare" ); | ||
RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition, 123 ); | ||
|
||
session.persist( popularBook ); | ||
session.persist( rareBook ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void test(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
List<Integer> rareBookCodes = session.createQuery( | ||
"select b.code from RareBook b where b.id = :id", | ||
Integer.class | ||
).setParameter( "id", RARE_BOOK_ID ).list(); | ||
|
||
assertThat( rareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
Integer code = rareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( 123 ); | ||
} | ||
); | ||
|
||
scope.inTransaction( | ||
session -> { | ||
List<String> populareBookCodes = session.createQuery( | ||
"select b.code from PopularBook b where b.id = :id", | ||
String.class | ||
).setParameter( "id", POPULAR_BOOK_ID ).list(); | ||
|
||
assertThat( populareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
String code = populareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( "POP" ); | ||
} | ||
); | ||
} | ||
|
||
@Embeddable | ||
public static class Edition { | ||
private String name; | ||
|
||
public Edition() { | ||
} | ||
|
||
public Edition(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@MappedSuperclass | ||
public static abstract class Book<T> { | ||
private Edition edition; | ||
|
||
@Column(name = "CODE_COLUMN") | ||
private T code; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(Edition edition, T code) { | ||
this.edition = edition; | ||
this.code = code; | ||
} | ||
} | ||
|
||
|
||
@Entity(name = "PopularBook") | ||
public static class PopularBook extends Book<String> { | ||
|
||
@Id | ||
private Long id; | ||
|
||
public PopularBook() { | ||
} | ||
|
||
public PopularBook(Long id, Edition edition, String code) { | ||
super( edition, code ); | ||
this.id = id; | ||
} | ||
} | ||
|
||
@Entity(name = "RareBook") | ||
public static class RareBook extends Book<Integer> { | ||
|
||
@Id | ||
private Long id; | ||
|
||
public RareBook() { | ||
} | ||
|
||
public RareBook(Long id, Edition edition, Integer code) { | ||
super( edition, code ); | ||
this.id = id; | ||
} | ||
|
||
} | ||
} |
180 changes: 180 additions & 0 deletions
180
...ernate/orm/test/annotations/embeddables/EmbeddableWithGenericAndMappedSuperClassTest.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,180 @@ | ||
package org.hibernate.orm.test.annotations.embeddables; | ||
|
||
import java.util.List; | ||
|
||
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.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.DiscriminatorColumn; | ||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Inheritance; | ||
import jakarta.persistence.InheritanceType; | ||
import jakarta.persistence.MappedSuperclass; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
EmbeddableWithGenericAndMappedSuperClassTest.PopularBook.class, | ||
EmbeddableWithGenericAndMappedSuperClassTest.RareBook.class | ||
} | ||
) | ||
@SessionFactory | ||
public class EmbeddableWithGenericAndMappedSuperClassTest { | ||
|
||
private final static long POPULAR_BOOK_ID = 1l; | ||
private final static String POPULAR_BOOK_CODE = "POP"; | ||
private final static long RARE_BOOK_ID = 2l; | ||
private final static Integer RARE_BOOK_CODE = 123; | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
Edition popularEdition = new Edition<>( "Popular", POPULAR_BOOK_CODE ); | ||
PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition ); | ||
|
||
Edition rareEdition = new Edition<>( "Rare", RARE_BOOK_CODE ); | ||
RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition ); | ||
|
||
session.persist( popularBook ); | ||
session.persist( rareBook ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void test(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
List<Integer> rareBookCodes = session.createQuery( | ||
"select b.edition.code from RareBook b where b.id = :id", | ||
Integer.class | ||
).setParameter( "id", RARE_BOOK_ID ).list(); | ||
|
||
assertThat( rareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
Integer code = rareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( RARE_BOOK_CODE ); | ||
} | ||
); | ||
|
||
scope.inTransaction( | ||
session -> { | ||
List<Integer> rareBookCodes = session.createQuery( | ||
"select c.code from Base b left join treat(b as RareBook).edition c where b.id = :id", | ||
Integer.class | ||
).setParameter( "id", RARE_BOOK_ID ).list(); | ||
|
||
assertThat( rareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
Integer code = rareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( RARE_BOOK_CODE ); | ||
} | ||
); | ||
|
||
scope.inTransaction( | ||
session -> { | ||
List<String> populareBookCodes = session.createQuery( | ||
"select b.edition.code from PopularBook b where b.id = :id", | ||
String.class | ||
).setParameter( "id", POPULAR_BOOK_ID ).list(); | ||
|
||
assertThat( populareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
String code = populareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( POPULAR_BOOK_CODE ); | ||
} | ||
); | ||
|
||
scope.inTransaction( | ||
session -> { | ||
List<String> rareBookCodes = session.createQuery( | ||
"select c.code from Base b left join treat(b as PopularBook).edition c where b.id = :id", | ||
String.class | ||
).setParameter( "id", POPULAR_BOOK_ID ).list(); | ||
|
||
assertThat( rareBookCodes.size() ).isEqualTo( 1 ); | ||
|
||
String code = rareBookCodes.get( 0 ); | ||
assertThat( code ).isEqualTo( POPULAR_BOOK_CODE ); | ||
} | ||
); | ||
} | ||
|
||
@Embeddable | ||
public static class Edition<T> { | ||
private String editorName; | ||
|
||
@Column(name = "CODE_COLUMN") | ||
private T code; | ||
|
||
public Edition() { | ||
} | ||
|
||
public Edition(String editorName, T code) { | ||
this.editorName = editorName; | ||
this.code = code; | ||
} | ||
} | ||
|
||
@Entity(name = "Base") | ||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
@DiscriminatorColumn(name = "PROP_TYPE") | ||
public abstract static class Base { | ||
@Id | ||
private Long id; | ||
|
||
|
||
public Base() { | ||
} | ||
|
||
public Base(Long id) { | ||
this.id = id; | ||
} | ||
} | ||
|
||
@MappedSuperclass | ||
public static abstract class Book<T> extends Base { | ||
private Edition<T> edition; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(Long id, Edition<T> edition) { | ||
super( id ); | ||
this.edition = edition; | ||
} | ||
} | ||
|
||
|
||
@Entity(name = "PopularBook") | ||
public static class PopularBook extends Book<String> { | ||
|
||
|
||
public PopularBook() { | ||
} | ||
|
||
public PopularBook(Long id, Edition<String> edition) { | ||
super( id, edition ); | ||
} | ||
} | ||
|
||
@Entity(name = "RareBook") | ||
public static class RareBook extends Book<Integer> { | ||
|
||
public RareBook() { | ||
} | ||
|
||
public RareBook(Long id, Edition<Integer> edition) { | ||
super( id, edition ); | ||
} | ||
|
||
} | ||
} |