use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class EntityCacheTestClass method testAccessConnectionWithCacheInvalidation.
@Test
@Disabled
public void testAccessConnectionWithCacheInvalidation() {
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().getConnection();
FetchPlanRepository viewRepository = AppBeans.get(FetchPlanRepository.class);
FetchPlan view = viewRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
persistence.getEntityManager().find(User.class, this.user.getId(), view);
tx.commit();
}
// User, Group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().getConnection();
FetchPlanRepository viewRepository = AppBeans.get(FetchPlanRepository.class);
FetchPlan view = viewRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
persistence.getEntityManager().find(User.class, this.user.getId(), view);
tx.commit();
}
// User, Group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
try (Transaction tx1 = persistence.getTransaction()) {
persistence.getEntityManager().getConnection();
tx1.commit();
}
FetchPlanRepository fetchPlanRepository = AppBeans.get(FetchPlanRepository.class);
FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
Query query = persistence.getEntityManager().createQuery("select u from test$User u where u.id = :id").setParameter("id", user.getId());
query.setView(View.copy((View) fetchPlan).setLoadPartialEntities(true));
((QueryImpl) query).setSingleResultExpected(true);
User userL = (User) query.getSingleResult();
// User userL = persistence.getEntityManager().find(User.class, user.getId(), view);
assertNotNull(userL);
tx.commit();
}
// User, Group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class EntityCacheTestClass method loadUserWithRoles.
private User loadUserWithRoles() throws Exception {
FetchPlan roleView = new View(Role.class).addProperty("name");
FetchPlan userRoleView = new View(UserRole.class).addProperty("role", roleView);
FetchPlan groupView = new View(Group.class).addProperty("name");
FetchPlan userView = new View(User.class).addProperty("login").addProperty("name").addProperty("userRoles", userRoleView).addProperty("group", groupView);
User user;
try (Transaction tx = persistence.createTransaction()) {
user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
assertNotNull(user);
tx.commit();
}
user = testSupport.reserialize(user);
Group g = user.getGroup();
assertEquals(this.group, g);
assertNotNull(g.getName());
assertFail(g::getParent);
user.getUserRoles().size();
return user;
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class EntityCacheTestClass method testFindWithView3.
@Test
public void testFindWithView3() throws Exception {
appender.clearMessages();
User user;
// no name in group
FetchPlan groupView = new View(Group.class, false);
FetchPlan userView = new View(User.class).addProperty("login").addProperty("group", groupView).setLoadPartialEntities(true);
try (Transaction tx = persistence.createTransaction()) {
user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
assertNotNull(user);
tx.commit();
}
user = testSupport.reserialize(user);
Group g = user.getGroup();
assertNotNull(user.getGroup());
assertEquals(this.group, g);
// due to caching, we load all attributes anyway
assertNotNull(g.getName());
// user, group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
// second time - from cache
try (Transaction tx = persistence.createTransaction()) {
user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
assertNotNull(user);
tx.commit();
}
user = testSupport.reserialize(user);
g = user.getGroup();
assertEquals(this.group, g);
assertNotNull(g.getName());
// user, group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
// name in group - from cache again
FetchPlan groupView1 = new View(Group.class, true).addProperty("name");
FetchPlan userView1 = new View(User.class).addProperty("login").addProperty("group", groupView1).setLoadPartialEntities(true);
try (Transaction tx = persistence.createTransaction()) {
user = persistence.getEntityManager().find(User.class, this.user.getId(), userView1);
assertNotNull(user);
tx.commit();
}
user = testSupport.reserialize(user);
g = user.getGroup();
assertEquals(this.group, g);
assertNotNull(g.getName());
// user, group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class QueryCacheTestClass method testLoadingWithNotPartialView.
@Test
public void testLoadingWithNotPartialView() throws Exception {
appender.clearMessages();
User user;
List<User> resultList;
// load with view: one parameter login
FetchPlan userLoginView = new View(User.class).addProperty("login");
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userLoginView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
// user
assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userLoginView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
// user
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
// load with view: group
FetchPlan groupView = new View(Group.class, false).addProperty("name");
FetchPlan userGroupView = new View(User.class).addProperty("group", groupView);
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userGroupView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT") && m.contains("TEST_GROUP")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userGroupView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userLoginView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertFail(user::getGroup);
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertFail(user::getGroup);
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setView(userGroupView);
resultList = query.getResultList();
assertEquals(resultList.size(), 2);
tx.commit();
}
user = testSupport.reserialize(resultList.get(0));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
user = testSupport.reserialize(resultList.get(1));
assertNotNull(user.getLogin());
assertNotNull(user.getName());
assertEquals(user.getGroup(), group);
assertEquals(user.getGroup().getName(), group.getName());
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToOneQuery.
@Test
public void testOneToOneQuery() {
System.out.println("===================== BEGIN testOneToOneQuery =====================");
// test fetchMode = AUTO
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"));
List<SoftDeleteOneToOneA> r = em.createQuery("select a from test$SoftDeleteOneToOneA a where a.name = :name", SoftDeleteOneToOneA.class).setParameter("name", "oneToOneA2").setView(view).getResultList();
assertEquals(1, r.size());
assertEquals(r.get(0).getB().getId(), oneToOneB2Id);
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOneQuery =====================");
}
Aggregations