Search in sources :

Example 51 with Transaction

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

the class FetchJoinTest method testNotLoadingCustomer.

@Test
public void testNotLoadingCustomer() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        View partyView = new View(Party.class).addProperty("name");
        View productView = new View(Product.class).addProperty("name");
        View customerView = new View(Customer.class).addProperty("customerNumber").addProperty("party", partyView);
        View salesPersonView = new View(SalesPerson.class).addProperty("salespersonNumber").addProperty("party", partyView);
        View orderView = new View(Order.class).addProperty("orderNumber").addProperty("customer", customerView).addProperty("salesPerson", salesPersonView);
        View orderLineView = new View(OrderLine.class).addProperty("order", orderView).addProperty("product", productView);
        OrderLine reloadedOrderLine = em.find(OrderLine.class, orderLine.getId(), orderLineView);
        assertNotNull(reloadedOrderLine);
        assertNotNull(reloadedOrderLine.getOrder().getCustomer());
        assertEquals(partyCustomer, reloadedOrderLine.getOrder().getCustomer().getParty());
        assertNotNull(reloadedOrderLine.getOrder().getSalesPerson());
        assertEquals(partyPerson, reloadedOrderLine.getOrder().getSalesPerson().getParty());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Example 52 with Transaction

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

the class CompositeKeyTest method testOperations.

@Test
public void testOperations() throws Exception {
    CompositeKeyEntity foo = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey = metadata.create(EntityKey.class);
    entityKey.setTenant(1);
    entityKey.setEntityId(10L);
    foo.setId(entityKey);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    CompositeKeyEntity loadedFoo;
    try (Transaction tx = persistence.createTransaction()) {
        loadedFoo = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        tx.commit();
    }
    assertNotNull(loadedFoo);
    assertEquals(foo, loadedFoo);
    loadedFoo.setName("bar");
    CompositeKeyEntity bar;
    try (Transaction tx = persistence.createTransaction()) {
        bar = persistence.getEntityManager().merge(loadedFoo);
        tx.commit();
    }
    assertEquals(foo, bar);
    CompositeKeyEntity loadedBar;
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        tx.commit();
    }
    assertNotNull(loadedBar);
    assertEquals("bar", loadedBar.getName());
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        persistence.getEntityManager().remove(loadedBar);
        tx.commit();
    }
    assertTrue(BaseEntityInternalAccess.isRemoved(loadedBar));
    try (Transaction tx = persistence.createTransaction()) {
        loadedBar = persistence.getEntityManager().find(CompositeKeyEntity.class, entityKey);
        assertNull(loadedBar);
        tx.commit();
    }
}
Also used : EntityKey(com.haulmont.cuba.testmodel.primary_keys.EntityKey) Transaction(com.haulmont.cuba.core.Transaction) CompositeKeyEntity(com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity) Test(org.junit.jupiter.api.Test)

Example 53 with Transaction

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

the class EntityManagerTest method testMerge.

@Test
public void testMerge() throws Exception {
    UUID newUserId = UUID.randomUUID();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = new User();
        user.setId(newUserId);
        user.setName("testMerge");
        user.setLogin("testMerge");
        user.setPassword("testMerge");
        user.setGroup(em.getReference(Group.class, groupId));
        user = em.merge(user);
        User userFromPersistentContext = em.find(User.class, newUserId);
        assertEquals(user, userFromPersistentContext);
        tx.commit();
    } finally {
        tx.end();
        cont.deleteRecord("SEC_USER", newUserId);
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 54 with Transaction

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

the class EntityManagerTest method testFindSeparateViews.

@Test
public void testFindSeparateViews() throws Exception {
    View view1 = new View(User.class, false).addProperty("name").addProperty("login");
    View view2 = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    User user1, user2;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        user1 = em.find(User.class, userId, view1);
        user2 = em.find(User.class, user2Id, view2);
        tx.commit();
    }
    user1 = reserialize(user1);
    user2 = reserialize(user2);
    assertNotNull(user1);
    assertNotNull(user1.getCreatedBy());
    try {
        user1.getGroup();
        fail();
    } catch (Exception ignored) {
    }
    assertNotNull(user2);
    assertNotNull(user2.getCreatedBy());
    assertNotNull(user2.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.jupiter.api.Test)

Example 55 with Transaction

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

the class EntityManagerTest method testQueryView.

@Test
public void testQueryView() throws Exception {
    View view = new View(User.class, "testQueryView", false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class, false).addProperty("name"));
    ((AbstractViewRepository) cont.metadata().getViewRepository()).storeView(cont.metadata().getSession().getClassNN(User.class), view);
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
        query.setParameter(1, userId);
        query.setViewName("testQueryView");
        user = query.getSingleResult();
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) 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