Search in sources :

Example 56 with Transaction

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

the class EntityManagerContextTest method testCommitRetaining.

@Test
public void testCommitRetaining() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager();
        EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
        ctx.setAttribute(ATTR, obj1);
        ctx = cont.persistence().getEntityManagerContext();
        assertTrue(ctx.getAttribute(ATTR) == obj1);
        tx.commitRetaining();
        cont.persistence().getEntityManager();
        ctx = cont.persistence().getEntityManagerContext();
        assertNull(ctx.getAttribute(ATTR));
        ctx.setAttribute(ATTR, obj2);
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager();
        EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
        Object obj = ctx.getAttribute(ATTR);
        assertNull(obj);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Example 57 with Transaction

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

the class EntityManagerContextTest method test.

@Test
public void test() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager();
        EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
        ctx.setAttribute(ATTR, obj1);
        cont.persistence().getEntityManager();
        ctx = cont.persistence().getEntityManagerContext();
        assertTrue(ctx.getAttribute(ATTR) == obj1);
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager();
        EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
        Object obj = ctx.getAttribute(ATTR);
        assertNull(obj);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Example 58 with Transaction

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

the class EntityFetcher method fetchReloaded.

protected void fetchReloaded(Entity entity, View view, Map<Instance, Set<View>> visited, boolean optimizeForDetached, Consumer<Entity> managedEntityConsumer) {
    if (!optimizeForDetached || needReloading(entity, view)) {
        if (log.isTraceEnabled()) {
            log.trace("Object " + entity + " is detached, loading it");
        }
        String storeName = metadata.getTools().getStoreName(entity.getMetaClass());
        if (storeName != null) {
            try (Transaction tx = persistence.getTransaction(storeName)) {
                EntityManager em = persistence.getEntityManager(storeName);
                // noinspection unchecked
                Entity managed = em.find(entity.getClass(), entity.getId());
                if (managed != null) {
                    // the instance here can be null if it has been deleted
                    managedEntityConsumer.accept(managed);
                    fetch(managed, view, visited, optimizeForDetached);
                }
                tx.commit();
            }
        }
    }
}
Also used : EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 59 with Transaction

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

the class FileLoaderImpl method checkIfFileDescriptorExists.

protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException {
    try (Transaction tx = persistence.getTransaction()) {
        FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId());
        if (existingFile == null || entityStates.isDeleted(existingFile)) {
            throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName());
        }
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 60 with Transaction

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

the class Emailer method migrateAttachmentsBatch.

protected int migrateAttachmentsBatch() {
    List<SendingAttachment> resultList;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String qstr = "select a from sys$SendingAttachment a where a.content is not null";
        TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
        query.setMaxResults(50);
        query.setViewName(View.MINIMAL);
        resultList = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    if (!resultList.isEmpty()) {
        emailer.migrateAttachmentsToFileStorage(resultList);
    }
    return resultList.size();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) Transaction(com.haulmont.cuba.core.Transaction)

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