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();
}
}
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();
}
}
Aggregations