Search in sources :

Example 1 with CompositeKeyEntity

use of com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity 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.Test)

Example 2 with CompositeKeyEntity

use of com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity in project cuba by cuba-platform.

the class CompositeKeyTest method testListParameter.

@Test
public void testListParameter() throws Exception {
    CompositeKeyEntity foo1 = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey1 = metadata.create(EntityKey.class);
    entityKey1.setTenant(1);
    entityKey1.setEntityId(10L);
    foo1.setId(entityKey1);
    foo1.setName("foo1");
    foo1.setEmail("foo1@mail.com");
    CompositeKeyEntity foo2 = metadata.create(CompositeKeyEntity.class);
    EntityKey entityKey2 = metadata.create(EntityKey.class);
    entityKey2.setTenant(2);
    entityKey2.setEntityId(20L);
    foo2.setId(entityKey2);
    foo2.setName("foo2");
    foo2.setEmail("foo2@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo1);
        persistence.getEntityManager().persist(foo2);
        tx.commit();
    }
    try (Transaction tx = persistence.createTransaction()) {
        Query query = persistence.getEntityManager().createQuery("select e from test$CompositeKeyEntity e " + "where e.id.tenant in :list1 and e.id.entityId in :list2");
        query.setParameter("list1", Arrays.asList(entityKey1.getTenant(), entityKey2.getTenant()));
        query.setParameter("list2", Arrays.asList(entityKey1.getEntityId(), entityKey2.getEntityId()));
        List resultList = query.getResultList();
        assertTrue(resultList.contains(foo1) && resultList.contains(foo2));
        tx.commit();
    }
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().remove(foo1);
        persistence.getEntityManager().remove(foo2);
        tx.commit();
    }
}
Also used : EntityKey(com.haulmont.cuba.testmodel.primary_keys.EntityKey) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List) CompositeKeyEntity(com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity) Test(org.junit.Test)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)2 CompositeKeyEntity (com.haulmont.cuba.testmodel.primary_keys.CompositeKeyEntity)2 EntityKey (com.haulmont.cuba.testmodel.primary_keys.EntityKey)2 Test (org.junit.Test)2 Query (com.haulmont.cuba.core.Query)1 List (java.util.List)1