Skip to content

Commit 7d95119

Browse files
committed
HBX-2789: Review WrapperFactory class and create only new Wrapper intances
- Interface method 'org.hibernate.tool.orm.jbt.api.wrp.SessionWrapper#createCriteria(Class<?>)' returns in instance of interface 'org.hibernate.tool.orm.jbt.api.wrp.QueryWrapper' - Adapt implementation of method 'org.hibernate.tool.orm.jbt.internal.factory.SessionWrapperFactory.SessionWrapperImpl#createCriteria(Class<?>)' - Adapt test method 'org.hibernate.tool.orm.jbt.internal.factory. SessionWrapperFactoryTest#testCreateCriteria()' Signed-off-by: Koen Aers <[email protected]>
1 parent 0c7704a commit 7d95119

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.hibernate.tool.orm.jbt.api.wrp;
22

3-
import jakarta.persistence.Query;
4-
53
public interface SessionWrapper extends Wrapper {
64

75
String getEntityName(Object o) ;
@@ -10,6 +8,6 @@ public interface SessionWrapper extends Wrapper {
108
boolean isOpen();
119
void close();
1210
boolean contains(Object o);
13-
Query createCriteria(Class<?> c);
11+
QueryWrapper createCriteria(Class<?> c);
1412

1513
}

jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/SessionWrapperFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import org.hibernate.Session;
44
import org.hibernate.SessionFactory;
5+
import org.hibernate.query.Query;
56
import org.hibernate.tool.orm.jbt.api.wrp.QueryWrapper;
67
import org.hibernate.tool.orm.jbt.api.wrp.SessionFactoryWrapper;
78
import org.hibernate.tool.orm.jbt.api.wrp.SessionWrapper;
89

9-
import jakarta.persistence.Query;
1010
import jakarta.persistence.criteria.CriteriaBuilder;
1111
import jakarta.persistence.criteria.CriteriaQuery;
1212
import jakarta.persistence.criteria.Root;
@@ -72,12 +72,12 @@ public boolean contains(Object o) {
7272
}
7373

7474
@Override
75-
public Query createCriteria(Class<?> c) {
75+
public QueryWrapper createCriteria(Class<?> c) {
7676
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
7777
CriteriaQuery<?> criteriaQuery = criteriaBuilder.createQuery(c);
78-
Root root = criteriaQuery.from(c);
79-
criteriaQuery.select(root);
80-
return ((Session)getWrappedObject()).createQuery(criteriaQuery);
78+
criteriaQuery.select((Root)criteriaQuery.from(c));
79+
Query<?> query = ((Session)getWrappedObject()).createQuery(criteriaQuery);
80+
return QueryWrapperFactory.createQueryWrapper(query);
8181
}
8282

8383
}

jbt/src/test/java/org/hibernate/tool/orm/jbt/api/wrp/SessionWrapperTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.SessionFactory;
1717
import org.hibernate.cfg.AvailableSettings;
1818
import org.hibernate.cfg.Configuration;
19+
import org.hibernate.query.Query;
1920
import org.hibernate.tool.orm.jbt.internal.factory.SessionWrapperFactory;
2021
import org.hibernate.tool.orm.jbt.util.MockConnectionProvider;
2122
import org.hibernate.tool.orm.jbt.util.MockDialect;
@@ -126,8 +127,10 @@ public void testContains() {
126127

127128
@Test
128129
public void testCreateCriteria() {
129-
jakarta.persistence.Query query = sessionWrapper.createCriteria(Foo.class);
130-
assertNotNull(query);
130+
QueryWrapper queryWrapper = sessionWrapper.createCriteria(Foo.class);
131+
assertNotNull(queryWrapper);
132+
Query<?> q = (Query)queryWrapper.getWrappedObject();
133+
assertSame(q.getSession(), sessionWrapper.getWrappedObject());
131134
}
132135

133136
}

0 commit comments

Comments
 (0)