use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class SoftDeleteTest method testManyToOne.
@Test
public void testManyToOne() throws SQLException {
System.out.println("===================== BEGIN testManyToOne =====================");
QueryRunner queryRunner = new QueryRunner(persistence.getDataSource());
queryRunner.update("update SEC_GROUP set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", new Object[] { groupId.toString() });
// test without view
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
User user = em.find(User.class, userId);
Group group = user.getGroup();
tx.commit();
assertNotNull(group);
assertTrue(group.isDeleted());
}
View view;
// test fetchMode = AUTO (JOIN is used)
view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
User user = em.find(User.class, userId, view);
Group group = user.getGroup();
tx.commit();
assertNotNull(group);
assertTrue(group.isDeleted());
}
// test fetchMode = UNDEFINED
view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.UNDEFINED);
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
User user = em.find(User.class, userId, view);
Group group = user.getGroup();
tx.commit();
assertNotNull(group);
assertTrue(group.isDeleted());
}
// test fetchMode = BATCH
view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.BATCH);
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
User user = em.find(User.class, userId, view);
Group group = user.getGroup();
tx.commit();
assertNotNull(group);
assertTrue(group.isDeleted());
}
System.out.println("===================== END testManyToOne =====================");
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class QueryResultTest method test.
@Test
public void test() {
Transaction tx;
javax.persistence.EntityManager emDelegate;
EntityManager em;
Query query;
UUID sessionId = UUID.randomUUID();
int queryKey = 1;
tx = cont.persistence().createTransaction();
try {
emDelegate = cont.persistence().getEntityManager().getDelegate();
QueryResult queryResult = new QueryResult();
queryResult.setSessionId(sessionId);
queryResult.setQueryKey(queryKey);
queryResult.setEntityId(UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
emDelegate.persist(queryResult);
tx.commitRetaining();
em = cont.persistence().getEntityManager();
query = em.createQuery("select u from sec$User u, sys$QueryResult qr " + "where qr.entityId = u.id and qr.sessionId = ?1 and qr.queryKey = ?2");
query.setParameter(1, sessionId);
query.setParameter(2, queryKey);
query.setView(new View(User.class).addProperty("login").addProperty("name").addProperty("group", new View(Group.class).addProperty("name")));
// OpenJPAQuery openJPAQuery = (OpenJPAQuery) query.getDelegate();
// Map params = new HashMap();
// params.put(1, sessionId);
// params.put(2, queryKey);
// String[] dataStoreActions = openJPAQuery.getDataStoreActions(params);
//
// System.out.println(dataStoreActions);
List<User> list = query.getResultList();
assertEquals(1, list.size());
User user = list.get(0);
assertEquals("admin", user.getLogin());
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class UnfetchedAttributeTest method testSet.
@Test
public void testSet() throws Exception {
User user = null;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Query q = em.createQuery("select u from sec$User u where u.id = ?1");
q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))));
q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
List<User> list = q.getResultList();
if (!list.isEmpty()) {
user = list.get(0);
// set value to not present in view
user.setGroup(group);
}
tx.commit();
} finally {
tx.end();
}
user = reserialize(user);
assertNotNull(user);
assertNotNull(user.getUserRoles());
user.getUserRoles().size();
assertNotNull(user.getGroup());
}
use of com.haulmont.cuba.core.global.View 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.global.View 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