6 - SQM based on JPA type system
This commit is contained in:
parent
711aaec3c9
commit
588f11684d
|
@ -6,10 +6,11 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.manytomany;
|
package org.hibernate.test.manytomany;
|
||||||
|
|
||||||
import org.hibernate.FetchMode;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.JoinType;
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
@ -36,94 +37,105 @@ public class ManyToManyTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testManyToManyWithFormula() {
|
public void testManyToManyWithFormula() {
|
||||||
Session s = openSession();
|
User user = new User("gavin", "jboss");
|
||||||
Transaction t = s.beginTransaction();
|
Group seamGroup = new Group("seam", "jboss");
|
||||||
User gavin = new User("gavin", "jboss");
|
Group hbGroup = new Group("hibernate", "jboss");
|
||||||
Group seam = new Group("seam", "jboss");
|
inTransaction(
|
||||||
Group hb = new Group("hibernate", "jboss");
|
s -> {
|
||||||
gavin.getGroups().add(seam);
|
user.getGroups().add(seamGroup);
|
||||||
gavin.getGroups().add(hb);
|
user.getGroups().add(hbGroup);
|
||||||
seam.getUsers().add(gavin);
|
seamGroup.getUsers().add(user);
|
||||||
hb.getUsers().add(gavin);
|
hbGroup.getUsers().add(user);
|
||||||
s.persist(gavin);
|
s.persist(user);
|
||||||
s.persist(seam);
|
s.persist(seamGroup);
|
||||||
s.persist(hb);
|
s.persist(hbGroup);
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
t = s.beginTransaction();
|
s -> {
|
||||||
gavin = (User) s.get(User.class, gavin);
|
User gavin = s.get(User.class, user);
|
||||||
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
|
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
|
||||||
assertEquals( 2, gavin.getGroups().size() );
|
assertEquals( 2, gavin.getGroups().size() );
|
||||||
hb = (Group) s.get(Group.class, hb);
|
Group hb = s.get(Group.class, hbGroup);
|
||||||
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
|
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
|
||||||
assertEquals( 1, hb.getUsers().size() );
|
assertEquals( 1, hb.getUsers().size() );
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
t = s.beginTransaction();
|
s -> {
|
||||||
gavin = (User) s.createCriteria(User.class)
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
.setFetchMode("groups", FetchMode.JOIN)
|
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
|
||||||
.uniqueResult();
|
criteria.from( User.class ).fetch( "groups", JoinType.LEFT );
|
||||||
|
|
||||||
|
User gavin = s.createQuery( criteria ).uniqueResult();
|
||||||
|
// User gavin = (User) s.createCriteria(User.class)
|
||||||
|
// .setFetchMode("groups", FetchMode.JOIN)
|
||||||
|
// .uniqueResult();
|
||||||
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
||||||
assertEquals( 2, gavin.getGroups().size() );
|
assertEquals( 2, gavin.getGroups().size() );
|
||||||
Group group = (Group) gavin.getGroups().iterator().next();
|
Group group = (Group) gavin.getGroups().iterator().next();
|
||||||
assertFalse( Hibernate.isInitialized( group.getUsers() ) );
|
assertFalse( Hibernate.isInitialized( group.getUsers() ) );
|
||||||
assertEquals( 1, group.getUsers().size() );
|
assertEquals( 1, group.getUsers().size() );
|
||||||
t.commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
s = openSession();
|
}
|
||||||
t = s.beginTransaction();
|
);
|
||||||
gavin = (User) s.createCriteria(User.class)
|
|
||||||
.setFetchMode("groups", FetchMode.JOIN)
|
inTransaction(
|
||||||
.setFetchMode("groups.users", FetchMode.JOIN)
|
s -> {
|
||||||
.uniqueResult();
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
|
||||||
|
criteria.from( User.class ).fetch( "groups", JoinType.LEFT ).fetch( "users", JoinType.LEFT );
|
||||||
|
|
||||||
|
User gavin = s.createQuery( criteria ).uniqueResult();
|
||||||
|
// User gavin = (User) s.createCriteria(User.class)
|
||||||
|
// .setFetchMode("groups", FetchMode.JOIN)
|
||||||
|
// .setFetchMode("groups.users", FetchMode.JOIN)
|
||||||
|
// .uniqueResult();
|
||||||
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
||||||
assertEquals( 2, gavin.getGroups().size() );
|
assertEquals( 2, gavin.getGroups().size() );
|
||||||
group = (Group) gavin.getGroups().iterator().next();
|
Group group = (Group) gavin.getGroups().iterator().next();
|
||||||
assertTrue( Hibernate.isInitialized( group.getUsers() ) );
|
assertTrue( Hibernate.isInitialized( group.getUsers() ) );
|
||||||
assertEquals( 1, group.getUsers().size() );
|
assertEquals( 1, group.getUsers().size() );
|
||||||
t.commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
s = openSession();
|
}
|
||||||
t = s.beginTransaction();
|
);
|
||||||
gavin = (User) s.createQuery("from User u join fetch u.groups g join fetch g.users").uniqueResult();
|
|
||||||
|
inTransaction(
|
||||||
|
s -> {
|
||||||
|
User gavin = (User) s.createQuery("from User u join fetch u.groups g join fetch g.users").uniqueResult();
|
||||||
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
|
||||||
assertEquals( 2, gavin.getGroups().size() );
|
assertEquals( 2, gavin.getGroups().size() );
|
||||||
group = (Group) gavin.getGroups().iterator().next();
|
Group group = (Group) gavin.getGroups().iterator().next();
|
||||||
assertTrue( Hibernate.isInitialized( group.getUsers() ) );
|
assertTrue( Hibernate.isInitialized( group.getUsers() ) );
|
||||||
assertEquals( 1, group.getUsers().size() );
|
assertEquals( 1, group.getUsers().size() );
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
t = s.beginTransaction();
|
s -> {
|
||||||
gavin = (User) s.get(User.class, gavin);
|
User gavin = s.get(User.class, user);
|
||||||
hb = (Group) s.get(Group.class, hb);
|
Group hb = s.get(Group.class, hbGroup);
|
||||||
gavin.getGroups().remove(hb);
|
gavin.getGroups().remove(hb);
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
t = s.beginTransaction();
|
s -> {
|
||||||
gavin = (User) s.get(User.class, gavin);
|
User gavin = s.get(User.class, user);
|
||||||
assertEquals( gavin.getGroups().size(), 1 );
|
assertEquals( gavin.getGroups().size(), 1 );
|
||||||
hb = (Group) s.get(Group.class, hb);
|
Group hb = s.get(Group.class, hbGroup);
|
||||||
assertEquals( hb.getUsers().size(), 0 );
|
assertEquals( hb.getUsers().size(), 0 );
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
inTransaction(
|
||||||
s = openSession();
|
s -> {
|
||||||
t = s.beginTransaction();
|
s.delete(user);
|
||||||
s.delete(gavin);
|
|
||||||
s.flush();
|
s.flush();
|
||||||
s.createQuery("delete from Group").executeUpdate();
|
s.createQuery("delete from Group").executeUpdate();
|
||||||
t.commit();
|
}
|
||||||
s.close();
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -15,6 +15,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
import javax.persistence.SharedCacheMode;
|
import javax.persistence.SharedCacheMode;
|
||||||
|
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
|
@ -510,6 +511,10 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
|
||||||
TransactionUtil2.inTransaction( sessionFactory(), action );
|
TransactionUtil2.inTransaction( sessionFactory(), action );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected <R> R inTransactionReturn(Function<SessionImplementor, R> function) {
|
||||||
|
return TransactionUtil2.inTransactionReturn( sessionFactory(), function );
|
||||||
|
}
|
||||||
|
|
||||||
protected void inTransaction(SessionImplementor session, Consumer<SessionImplementor> action) {
|
protected void inTransaction(SessionImplementor session, Consumer<SessionImplementor> action) {
|
||||||
TransactionUtil2.inTransaction( session, action );
|
TransactionUtil2.inTransaction( session, action );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue