use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class SerializationTest method testDetachedSerialization.
@Test
public void testDetachedSerialization() throws Exception {
View view = getView();
User user;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
user = em.find(User.class, userId, view);
assertNotNull(user);
tx.commit();
} finally {
tx.end();
}
KryoSerialization kryoSerialization = new KryoSerialization();
User kryoUser = (User) kryoSerialization.deserialize(kryoSerialization.serialize(user));
StandardSerialization standardSerialization = new StandardSerialization();
User standardUser = (User) standardSerialization.deserialize(standardSerialization.serialize(user));
assertEquals(standardUser, kryoUser);
assertEquals(standardUser.getLogin(), kryoUser.getLogin());
assertEquals(standardUser.getName(), kryoUser.getName());
assertEquals(standardUser.getPosition(), kryoUser.getPosition());
assertEquals(standardUser.getGroup(), kryoUser.getGroup());
assertEquals(standardUser.getGroup().getName(), kryoUser.getGroup().getName());
assertEquals(standardUser.getUserRoles().get(0), kryoUser.getUserRoles().get(0));
assertEquals(standardUser.getUserRoles().get(0).getRole(), kryoUser.getUserRoles().get(0).getRole());
assertEquals(standardUser.getUserRoles().get(0).getRole().getName(), kryoUser.getUserRoles().get(0).getRole().getName());
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class QueryCacheTestClass method testStaleData_UpdateParentEntity.
@Test
public void testStaleData_UpdateParentEntity() throws Exception {
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select f from sys$Folder f");
query.setCacheable(true);
query.getResultList();
tx.commit();
}
assertEquals(1, queryCache.size());
appender.clearMessages();
appFolder = cont.metadata().create(AppFolder.class);
appFolder.setName("new app folder");
try (Transaction tx = cont.persistence().createTransaction()) {
cont.entityManager().persist(appFolder);
tx.commit();
}
appender.clearMessages();
assertEquals(0, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select f from sys$Folder f");
query.setCacheable(true);
query.getResultList();
tx.commit();
}
assertEquals(1, queryCache.size());
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class QueryCacheTestClass method testNotEntityQuery.
@Test
public void testNotEntityQuery() throws Exception {
appender.clearMessages();
assertEquals(0, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select u, u.name from sec$User u where u.login = :login");
query.setCacheable(true);
query.setParameter("login", "ECTest-" + user.getId());
query.getResultList();
tx.commit();
}
assertEquals(0, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select u.name from sec$User u where u.login = :login");
query.setCacheable(true);
query.setParameter("login", "ECTest-" + user.getId());
query.getResultList();
tx.commit();
}
assertEquals(0, queryCache.size());
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class QueryCacheTestClass method testStaleData_UpdateDependentEntity.
@Test
public void testStaleData_UpdateDependentEntity() throws Exception {
appender.clearMessages();
getResultListUserByRole("TestRole");
// User, Group
assertEquals(5, appender.filterMessages(m -> m.contains("> SELECT")).count());
assertEquals(1, queryCache.size());
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
Role updatedRole = cont.entityManager().find(Role.class, role.getId());
updatedRole.setDescription("New Role Description");
tx.commit();
}
appender.clearMessages();
assertEquals(0, queryCache.size());
getResultListUserByRole("TestRole");
assertEquals(1, queryCache.size());
assertEquals(3, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class QueryCacheTestClass method testQueryPaging.
@Test
public void testQueryPaging() throws Exception {
appender.clearMessages();
assertEquals(0, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setViewName("user.browse");
query.setFirstResult(0);
query.setMaxResults(1);
List<User> result = query.getResultList();
assertEquals(result.size(), 1);
assertUserBrowseView(result.get(0));
assertEquals(this.user.getLogin(), result.get(0).getLogin());
assertEquals(this.group, result.get(0).getGroup());
tx.commit();
}
// User, Group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
assertEquals(1, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setViewName("user.browse");
query.setFirstResult(0);
query.setMaxResults(1);
List<User> result = query.getResultList();
assertEquals(result.size(), 1);
assertUserBrowseView(result.get(0));
assertEquals(this.user.getLogin(), result.get(0).getLogin());
assertEquals(this.group, result.get(0).getGroup());
tx.commit();
}
// User, Group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
assertEquals(1, queryCache.size());
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setViewName("user.browse");
query.setFirstResult(1);
query.setMaxResults(1);
List<User> result = query.getResultList();
assertEquals(result.size(), 1);
assertUserBrowseView(result.get(0));
assertEquals(this.user2.getLogin(), result.get(0).getLogin());
assertEquals(this.group, result.get(0).getGroup());
tx.commit();
}
// User, Group
assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
assertEquals(2, queryCache.size());
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
query.setCacheable(true);
query.setViewName("user.browse");
query.setFirstResult(1);
query.setMaxResults(1);
List<User> result = query.getResultList();
assertEquals(result.size(), 1);
assertUserBrowseView(result.get(0));
assertEquals(this.user2.getLogin(), result.get(0).getLogin());
assertEquals(this.group, result.get(0).getGroup());
tx.commit();
}
// User, Group
assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
assertEquals(2, queryCache.size());
appender.clearMessages();
}
Aggregations