77package org .hibernate .orm .test .mapping ;
88
99import java .io .Serializable ;
10+ import java .util .List ;
1011import java .util .Objects ;
12+
13+ import org .hibernate .testing .orm .junit .DomainModel ;
14+ import org .hibernate .testing .orm .junit .Jira ;
15+ import org .hibernate .testing .orm .junit .SessionFactory ;
16+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
17+ import org .junit .jupiter .api .AfterAll ;
18+ import org .junit .jupiter .api .BeforeAll ;
19+ import org .junit .jupiter .api .Test ;
20+
21+ import jakarta .persistence .Column ;
1122import jakarta .persistence .Entity ;
1223import jakarta .persistence .Id ;
1324import jakarta .persistence .IdClass ;
1425import jakarta .persistence .MappedSuperclass ;
26+ import jakarta .persistence .criteria .CriteriaBuilder ;
27+ import jakarta .persistence .criteria .CriteriaQuery ;
28+ import jakarta .persistence .criteria .Path ;
29+ import jakarta .persistence .criteria .Root ;
1530
16- import org .hibernate .testing .TestForIssue ;
17- import org .hibernate .testing .orm .junit .DomainModel ;
18- import org .hibernate .testing .orm .junit .SessionFactory ;
19- import org .junit .jupiter .api .Test ;
31+ import static org .assertj .core .api .Assertions .assertThat ;
2032
21- @ TestForIssue (jiraKey = "HHH-14499" )
2233@ DomainModel (
2334 annotatedClasses = {
2435 MappedSuperclassWithGenericsTest .IntermediateAbstractMapped .class ,
2536 MappedSuperclassWithGenericsTest .BaseEntity .class ,
2637 MappedSuperclassWithGenericsTest .AbstractGenericMappedSuperType .class ,
38+ MappedSuperclassWithGenericsTest .SimpleEntity .class ,
39+ MappedSuperclassWithGenericsTest .GenericIdBaseEntity .class
2740 }
2841)
2942@ SessionFactory
3043public class MappedSuperclassWithGenericsTest {
31-
3244 @ Test
45+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-14499" )
3346 public void testIt () {
3447
3548 }
3649
50+ @ Test
51+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18007" )
52+ void testSelectCriteriaGenericId (SessionFactoryScope scope ) {
53+ scope .inTransaction ( session -> {
54+ final CriteriaBuilder criteriaBuilder = session .getCriteriaBuilder ();
55+ final CriteriaQuery <Long > criteriaQuery = criteriaBuilder .createQuery ( Long .class );
56+ final Root <SimpleEntity > root = criteriaQuery .from ( SimpleEntity .class );
57+ final Path <Long > idPath = root .get ( "id" );
58+ criteriaQuery .select ( idPath );
59+ final List <Long > resultList = session .createQuery ( criteriaQuery ).getResultList ();
60+ assertThat ( resultList ).hasSize ( 1 ).containsOnly ( 1L );
61+ } );
62+ }
63+
64+ @ Test
65+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18007" )
66+ void testSelectGenericId (SessionFactoryScope scope ) {
67+ scope .inTransaction ( session -> {
68+ final List <Long > resultList = session .createQuery (
69+ "select e.id from SimpleEntity e" ,
70+ Long .class
71+ ).getResultList ();
72+ assertThat ( resultList ).hasSize ( 1 ).containsOnly ( 1L );
73+ } );
74+ }
75+
76+ @ BeforeAll
77+ public void setUp (SessionFactoryScope scope ) {
78+ scope .inTransaction ( session -> session .persist ( new SimpleEntity ( 1L , "simple_1" ) ) );
79+ }
80+
81+ @ AfterAll
82+ public void tearDown (SessionFactoryScope scope ) {
83+ scope .inTransaction ( session -> session .createMutationQuery ( "delete from SimpleEntity" ).executeUpdate () );
84+ }
85+
3786 @ MappedSuperclass
3887 public static abstract class AbstractGenericMappedSuperType <T > {
39-
4088 private T whateverType ;
41-
4289 }
4390
4491 @ MappedSuperclass
45- @ IdClass (PK .class )
92+ @ IdClass ( PK .class )
4693 public static abstract class IntermediateAbstractMapped <T > extends AbstractGenericMappedSuperType <T > {
47-
4894 @ Id
4995 private String keyOne ;
5096 @ Id
@@ -53,9 +99,8 @@ public static abstract class IntermediateAbstractMapped<T> extends AbstractGener
5399 private String keyThree ;
54100 }
55101
56- @ SuppressWarnings ("UnusedDeclaration" )
102+ @ SuppressWarnings ( "UnusedDeclaration" )
57103 public static class PK implements Serializable {
58-
59104 private String keyOne ;
60105 private String keyTwo ;
61106 private String keyThree ;
@@ -80,11 +125,37 @@ public int hashCode() {
80125 }
81126 }
82127
83- @ Entity (name = "BaseEntity" )
128+ @ Entity ( name = "BaseEntity" )
84129 public static class BaseEntity <T > extends IntermediateAbstractMapped <byte []> {
85-
86130 String aString ;
131+ }
132+
133+ @ MappedSuperclass
134+ public static class GenericIdBaseEntity <T extends Serializable > {
135+ @ Id
136+ private T id ;
137+
138+ protected GenericIdBaseEntity (T id ) {
139+ this .id = id ;
140+ }
87141
142+ public T getId () {
143+ return id ;
144+ }
88145 }
89146
147+ @ Entity ( name = "SimpleEntity" )
148+ public static class SimpleEntity extends GenericIdBaseEntity <Long > {
149+ @ Column
150+ private String string ;
151+
152+ public SimpleEntity () {
153+ super ( null );
154+ }
155+
156+ protected SimpleEntity (Long id , String string ) {
157+ super ( id );
158+ this .string = string ;
159+ }
160+ }
90161}
0 commit comments