3737import jakarta .persistence .Id ;
3838import jakarta .persistence .Inheritance ;
3939import jakarta .persistence .InheritanceType ;
40+ import jakarta .persistence .ManyToOne ;
4041import jakarta .persistence .MappedSuperclass ;
42+ import jakarta .persistence .OneToMany ;
4143import jakarta .persistence .SequenceGenerator ;
4244import jakarta .persistence .Tuple ;
4345import jakarta .persistence .TypedQuery ;
46+ import jakarta .persistence .criteria .CriteriaBuilder ;
4447import jakarta .persistence .criteria .Root ;
4548
4649import static org .junit .jupiter .api .Assertions .assertEquals ;
5558 CountQueryTests .LogSupport .class ,
5659 CountQueryTests .Contract .class ,
5760 CountQueryTests .SimpleDto .class ,
61+ CountQueryTests .BaseAttribs .class ,
62+ CountQueryTests .IdBased .class ,
63+ CountQueryTests .ParentEntity .class ,
64+ CountQueryTests .ChildEntity .class ,
5865 }
5966)
6067@ SessionFactory
@@ -240,6 +247,22 @@ public void testUnionQuery(SessionFactoryScope scope) {
240247 } );
241248 }
242249
250+ @ Test
251+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18357" )
252+ public void testJoinedEntityPath (SessionFactoryScope scope ) {
253+ scope .inTransaction ( session -> {
254+ final HibernateCriteriaBuilder cb = session .getCriteriaBuilder ();
255+ verifyCount ( session , cb .createQuery (
256+ "select c.id, c.parent from ChildEntity c" ,
257+ Tuple .class
258+ ) );
259+ verifyCount ( session , cb .createQuery (
260+ "select distinct c.id, c.parent from ChildEntity c" ,
261+ Tuple .class
262+ ) );
263+ } );
264+ }
265+
243266 @ BeforeEach
244267 public void prepareTestData (SessionFactoryScope scope ) {
245268 scope .inTransaction ( (session ) -> {
@@ -313,6 +336,15 @@ public void prepareTestData(SessionFactoryScope scope) {
313336 session .persist ( c4 );
314337 session .persist ( c8 );
315338 session .persist ( c7 );
339+
340+ final ParentEntity p1 = new ParentEntity ( "parent_1" , 1L );
341+ final ParentEntity p2 = new ParentEntity ( "parent_2" , 2L );
342+ final ChildEntity c1 = new ChildEntity ( "child_1" , 1L , p1 );
343+ final ChildEntity c2 = new ChildEntity ( "child_2" , 2L , p2 );
344+ session .persist ( p1 );
345+ session .persist ( p2 );
346+ session .persist ( c1 );
347+ session .persist ( c2 );
316348 } );
317349 }
318350
@@ -327,6 +359,8 @@ public void dropTestData(SessionFactoryScope scope) {
327359 scope .inTransaction ( (session ) -> {
328360 session .createMutationQuery ( "update Contact set alternativeContact = null" ).executeUpdate ();
329361 session .createMutationQuery ( "delete Contact" ).executeUpdate ();
362+ session .createMutationQuery ( "delete ChildEntity" ).executeUpdate ();
363+ session .createMutationQuery ( "delete ParentEntity" ).executeUpdate ();
330364 } );
331365 }
332366
@@ -356,4 +390,54 @@ public SimpleDto(String name) {
356390 this .name = name ;
357391 }
358392 }
393+
394+ @ MappedSuperclass
395+ static class BaseAttribs {
396+ private String description ;
397+
398+ public BaseAttribs () {
399+ }
400+
401+ public BaseAttribs (String description ) {
402+ this .description = description ;
403+ }
404+ }
405+
406+ @ MappedSuperclass
407+ static class IdBased extends BaseAttribs {
408+ @ Id
409+ private Long id ;
410+
411+ public IdBased () {
412+ }
413+
414+ public IdBased (String description , Long id ) {
415+ super ( description );
416+ this .id = id ;
417+ }
418+ }
419+
420+ @ Entity ( name = "ParentEntity" )
421+ static class ParentEntity extends IdBased {
422+ public ParentEntity () {
423+ }
424+
425+ public ParentEntity (String description , Long id ) {
426+ super ( description , id );
427+ }
428+ }
429+
430+ @ Entity ( name = "ChildEntity" )
431+ static class ChildEntity extends IdBased {
432+ @ ManyToOne
433+ private ParentEntity parent ;
434+
435+ public ChildEntity () {
436+ }
437+
438+ public ChildEntity (String description , Long id , ParentEntity parent ) {
439+ super ( description , id );
440+ this .parent = parent ;
441+ }
442+ }
359443}
0 commit comments