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