Search in sources :

Example 56 with EntityManager

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

the class SoftDeleteTest method testMultipleTransactions.

@Test
public void testMultipleTransactions() throws Exception {
    try (Transaction tx = persistence.createTransaction()) {
        assertTrue(CubaUtil.isSoftDeletion());
        EntityManager em = persistence.getEntityManager();
        em.setSoftDeletion(false);
        assertFalse(CubaUtil.isSoftDeletion());
        tx.commit();
    }
    try (Transaction tx = persistence.createTransaction()) {
        assertTrue(CubaUtil.isSoftDeletion());
        EntityManager em = persistence.getEntityManager();
        em.setSoftDeletion(false);
        assertFalse(CubaUtil.isSoftDeletion());
        try (Transaction tx1 = persistence.createTransaction()) {
            assertTrue(CubaUtil.isSoftDeletion());
            tx1.commit();
        }
        assertFalse(CubaUtil.isSoftDeletion());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 57 with EntityManager

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

the class SoftDeleteTest method testOneToOneLazy.

@Test
public void testOneToOneLazy() {
    System.out.println("===================== BEGIN testOneToOneLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneLazy =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SoftDeleteOneToOneA(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)

Example 58 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 59 with EntityManager

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

the class TestBeforeCommitTxListener method queryWithFlush.

private void queryWithFlush(Collection<Entity> managedEntities, EntityManager entityManager) {
    if (!managedEntities.stream().anyMatch(e -> e instanceof User && ((User) e).getLogin().startsWith("TxLstnrTst-")))
        return;
    TypedQuery<User> query = entityManager.createQuery("select u from sec$User u where u.login like ?1", User.class);
    query.setParameter(1, "TxLstnrTst-2-%");
    query.setFlushMode(FlushModeType.AUTO);
    User result = query.getFirstResult();
    assertNotNull(result);
}
Also used : TestSupport(com.haulmont.cuba.testsupport.TestSupport) EntityManager(com.haulmont.cuba.core.EntityManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) FlushModeType(javax.persistence.FlushModeType) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) Group(com.haulmont.cuba.security.entity.Group) TypedQuery(com.haulmont.cuba.core.TypedQuery) Component(org.springframework.stereotype.Component) PersistenceHelper(com.haulmont.cuba.core.global.PersistenceHelper) BeforeCommitTransactionListener(com.haulmont.cuba.core.listener.BeforeCommitTransactionListener) Assert.assertEquals(org.junit.Assert.assertEquals) Entity(com.haulmont.cuba.core.entity.Entity) User(com.haulmont.cuba.security.entity.User)

Example 60 with EntityManager

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

the class TestListenerUsingEntityManager method onBeforeInsert.

@Override
public void onBeforeInsert(Server entity, EntityManager entityManager) {
    EntityManager em = persistence.getEntityManager();
    FileDescriptor related = new FileDescriptor();
    related.setName("Related");
    System.out.println(">>>>> persist related: " + related.getId());
    em.persist(related);
    entity.setData(related.getId().toString());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4