|
17 | 17 | import org.hibernate.query.criteria.JpaCteCriteria;
|
18 | 18 | import org.hibernate.query.criteria.JpaEntityJoin;
|
19 | 19 | import org.hibernate.query.criteria.JpaJoin;
|
| 20 | +import org.hibernate.query.criteria.JpaJoinedFrom; |
20 | 21 | import org.hibernate.query.criteria.JpaParameterExpression;
|
21 | 22 | import org.hibernate.query.criteria.JpaRoot;
|
22 | 23 | import org.hibernate.query.criteria.JpaSubQuery;
|
23 | 24 | import org.hibernate.query.spi.QueryImplementor;
|
| 25 | +import org.hibernate.query.sqm.tree.SqmJoinType; |
24 | 26 | import org.hibernate.sql.ast.tree.cte.CteMaterialization;
|
25 | 27 | import org.hibernate.sql.ast.tree.cte.CteSearchClauseKind;
|
26 | 28 |
|
| 29 | +import org.hibernate.testing.orm.junit.Jira; |
27 | 30 | import org.hibernate.testing.orm.junit.SkipForDialect;
|
28 | 31 | import org.hibernate.testing.orm.domain.StandardDomainModel;
|
29 | 32 | import org.hibernate.testing.orm.domain.contacts.Address;
|
@@ -88,6 +91,47 @@ public void testBasic(SessionFactoryScope scope) {
|
88 | 91 | );
|
89 | 92 | }
|
90 | 93 |
|
| 94 | + @Test |
| 95 | + @Jira("https://hibernate.atlassian.net/browse/HHH-17897") |
| 96 | + public void testBasicJoined(SessionFactoryScope scope) { |
| 97 | + scope.inTransaction( |
| 98 | + session -> { |
| 99 | + final HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 100 | + final JpaCriteriaQuery<Tuple> cte = cb.createTupleQuery(); |
| 101 | + final JpaRoot<Contact> cteRoot = cte.from( Contact.class ); |
| 102 | + cte.multiselect( cteRoot.get( "id" ).alias( "id" ), cteRoot.get( "name" ).alias( "name" ) ); |
| 103 | + cte.where( cb.equal( cteRoot.get( "gender" ), Contact.Gender.FEMALE ) ); |
| 104 | + |
| 105 | + final JpaCriteriaQuery<Tuple> cq = cb.createTupleQuery(); |
| 106 | + final JpaCteCriteria<Tuple> femaleContacts = cq.with( cte ); |
| 107 | + |
| 108 | + final JpaRoot<Contact> root = cq.from( Contact.class ); |
| 109 | + final JpaJoinedFrom<?, Tuple> join = root.join( femaleContacts ); |
| 110 | + join.on( cb.equal( root.get( "id" ), join.get( "id" ) ) ); |
| 111 | + |
| 112 | + cq.multiselect( root.get( "id" ), root.get( "name" ) ); |
| 113 | + cq.orderBy( cb.asc( root.get( "id" ) ) ); |
| 114 | + |
| 115 | + final QueryImplementor<Tuple> query = session.createQuery( |
| 116 | + "with femaleContacts as (" + |
| 117 | + "select c.id id, c.name name from Contact c where c.gender = FEMALE" + |
| 118 | + ")" + |
| 119 | + "select c.id, c.name from Contact c join femaleContacts f on c.id = f.id order by c.id", |
| 120 | + Tuple.class |
| 121 | + ); |
| 122 | + verifySame( |
| 123 | + session.createQuery( cq ).getResultList(), |
| 124 | + query.getResultList(), |
| 125 | + list -> { |
| 126 | + assertEquals( 2, list.size() ); |
| 127 | + assertEquals( "Jane", list.get( 0 ).get( 1, Contact.Name.class ).getFirst() ); |
| 128 | + assertEquals( "Granny", list.get( 1 ).get( 1, Contact.Name.class ).getFirst() ); |
| 129 | + } |
| 130 | + ); |
| 131 | + } |
| 132 | + ); |
| 133 | + } |
| 134 | + |
91 | 135 | @Test
|
92 | 136 | public void testNested(SessionFactoryScope scope) {
|
93 | 137 | scope.inTransaction(
|
|
0 commit comments