Search in sources :

Example 46 with Transaction

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());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View)

Example 47 with Transaction

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());
}
Also used : AppFolder(com.haulmont.cuba.core.entity.AppFolder) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery) Test(org.junit.jupiter.api.Test)

Example 48 with Transaction

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());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery) Test(org.junit.jupiter.api.Test)

Example 49 with Transaction

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());
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Example 50 with Transaction

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();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)226 EntityManager (com.haulmont.cuba.core.EntityManager)142 Test (org.junit.jupiter.api.Test)59 Query (com.haulmont.cuba.core.Query)30 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)27 User (com.haulmont.cuba.security.entity.User)25 View (com.haulmont.cuba.core.global.View)22 BeforeEach (org.junit.jupiter.api.BeforeEach)18 TypedQuery (com.haulmont.cuba.core.TypedQuery)13 Group (com.haulmont.cuba.security.entity.Group)12 List (java.util.List)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Entity (com.haulmont.cuba.core.entity.Entity)7 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)7 ChildCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ChildCachedEntity)5 ParentCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ParentCachedEntity)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)5