use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class EntityManagerTest method testMergeWithOneToManyCollection.
@Test
public void testMergeWithOneToManyCollection() throws Exception {
User user = null;
UserRole userRole1 = null, userRole2 = null;
Role role1 = null, role2 = null;
try {
// creates user with one role
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
user = new User();
user.setName("testMerge1");
user.setLogin("testMerge1");
user.setPassword("testMerge1");
user.setGroup(em.getReference(Group.class, groupId));
em.persist(user);
role1 = new Role();
role1.setName("role1");
em.persist(role1);
role2 = new Role();
role2.setName("role2");
em.persist(role2);
userRole1 = new UserRole();
userRole1.setRole(role1);
userRole1.setUser(user);
em.persist(userRole1);
tx.commit();
} finally {
tx.end();
}
// creates new user role and merge it
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
userRole2 = new UserRole();
userRole2.setRole(role2);
userRole2.setUser(user);
em.merge(userRole2);
tx.commit();
} finally {
tx.end();
}
// check user versions, version shouldn't be changed
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertEquals((Integer) 1, em.find(User.class, user.getId()).getVersion());
tx.commit();
} finally {
tx.end();
}
} finally {
cont.deleteRecord(userRole1);
cont.deleteRecord(userRole2);
cont.deleteRecord(user);
cont.deleteRecord(role1);
cont.deleteRecord(role2);
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class EntityManagerTest method testFind.
// Commented in EL: EntityManager has no setView() method anymore
// public void testViewPropagation() throws Exception {
//
// View view = new View(User.class, false)
// .addProperty("name")
// .addProperty("login")
// .addProperty("group", new View(Group.class)
// .addProperty("name"));
//
// User user;
//
// Transaction tx = cont.persistence().createTransaction();
// try {
// EntityManager em = cont.persistence().getEntityManager();
// em.setView(view);
//
// TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
// query.setParameter(1, userId);
// user = query.getSingleResult();
//
// tx.commit();
// } finally {
// tx.end();
// }
//
// assertNull(user.getCreatedBy());
// assertNotNull(user.getGroup());
// }
@Test
public void testFind() throws Exception {
View view = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
User user;
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
user = em.find(User.class, userId, view);
tx.commit();
}
user = reserialize(user);
assertNotNull(user.getCreatedBy());
assertNotNull(user.getGroup());
}
use of com.haulmont.cuba.core.EntityManager 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());
}
use of com.haulmont.cuba.core.EntityManager 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.EntityManager in project cuba by cuba-platform.
the class EntityManagerTest method testReload.
@Test
public void testReload() throws Exception {
User originalUser, reloadedUser;
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
originalUser = em.find(User.class, userId, "user.browse");
tx.commit();
}
assertNotNull(originalUser);
assertFalse(PersistenceHelper.isLoaded(originalUser, "userRoles"));
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
reloadedUser = em.reload(originalUser, "user.edit");
tx.commit();
}
assertNotNull(reloadedUser);
assertTrue(PersistenceHelper.isLoaded(reloadedUser, "userRoles"));
assertTrue(originalUser != reloadedUser);
}
Aggregations