Search in sources :

Example 91 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteTest method testOneToManyLazy.

@Test
public void testOneToManyLazy() {
    System.out.println("===================== BEGIN testOneToManyLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToManyLazy =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 92 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteTest method testOneToOneMappedByLazy.

@Test
public void testOneToOneMappedByLazy() {
    System.out.println("===================== BEGIN testOneToOneMappedByLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB1Id);
        assertNotNull(oneToOneB);
        assertNull(oneToOneB.getA());
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneMappedByLazy =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) SoftDeleteOneToOneB(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)

Example 93 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteTest method testOneToMany_CleanupMode.

@Test
public void testOneToMany_CleanupMode() {
    System.out.println("===================== BEGIN testOneToMany_CleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        View view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("userRoles", new View(UserRole.class, "testView").addProperty("role", new View(Role.class, "testView").addProperty("name")));
        User user = em.find(User.class, userId, view);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(2, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToMany_CleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View)

Example 94 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteTest method testQuery_CleanupMode.

@Test
public void testQuery_CleanupMode() {
    System.out.println("===================== BEGIN testQuery_CleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Query query = em.createQuery("select r from sec$Role r where r.name = ?1");
        query.setParameter(1, "roleToBeDeleted");
        List<Role> list = query.getResultList();
        assertTrue(!list.isEmpty());
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testQuery_CleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 95 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteTest method testOneToMany_Query.

@Test
public void testOneToMany_Query() {
    System.out.println("===================== BEGIN testOneToMany_Query =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setParameter(1, userId);
        User user = (User) q.getSingleResult();
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToMany_Query =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

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