use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class OrmBehaviorTest method testPersistWithUnManagedAttribute.
/*
* Test that persist with un-managed attribute works (it didn't work in OpenJPA 2.2+ and worked in OpenJPA pre-2.2)
*/
@Test
public void testPersistWithUnManagedAttribute() throws Exception {
Group group = new Group();
groupId = group.getId();
group.setName("Old Name");
Transaction tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager().persist(group);
tx.commit();
} finally {
tx.end();
}
// Let's imagine that this entity was loaded with MyBatis
Group g = new Group();
g.setId(groupId);
g.setName("Old Name");
User user = new User();
userId = user.getId();
user.setLogin("typednativesqlquery");
user.setGroup(g);
user.setName("Test");
tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager().persist(user);
tx.commitRetaining();
user = cont.persistence().getEntityManager().find(User.class, userId, new View(User.class).addProperty("group"));
tx.commit();
} finally {
tx.end();
}
user = reserialize(user);
assertEquals(groupId, user.getGroup().getId());
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class PersistenceHelperTest method testCheckLoaded.
@Test
public void testCheckLoaded() {
Server server = new Server();
cont.persistence().runInTransaction((em) -> {
em.persist(server);
});
View view = new View(Server.class).addProperty("name").addProperty("data").setLoadPartialEntities(true);
Server reloadedServer = cont.persistence().callInTransaction((em) -> {
return em.find(Server.class, server.getId(), view);
});
// fine
PersistenceHelper.checkLoaded(reloadedServer, "name");
try {
PersistenceHelper.checkLoaded(reloadedServer, "data", "running");
Assert.fail("Must throw exception");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Server.running"));
}
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class PersistenceTest method testLoadByCombinedView.
@Test
public void testLoadByCombinedView() throws Exception {
User user;
Transaction tx = cont.persistence().createTransaction();
try {
// load by single view
EntityManager em = cont.persistence().getEntityManager();
user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true));
assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
assertFalse(cont.persistence().getTools().isLoaded(user, "name"));
tx.commitRetaining();
// load by combined view
em = cont.persistence().getEntityManager();
user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true), new View(User.class, false).addProperty("name").setLoadPartialEntities(true));
assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
assertTrue(cont.persistence().getTools().isLoaded(user, "name"));
tx.commitRetaining();
// load by complex combined view
em = cont.persistence().getEntityManager();
user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true), new View(User.class, false).addProperty("group", new View(Group.class).addProperty("name")).setLoadPartialEntities(true));
assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
assertFalse(cont.persistence().getTools().isLoaded(user, "name"));
assertTrue(cont.persistence().getTools().isLoaded(user, "group"));
assertTrue(cont.persistence().getTools().isLoaded(user.getGroup(), "name"));
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class UnfetchedAttributeTest method tearDown.
@After
public void tearDown() throws Exception {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
// load default 'Company' group
Query q = em.createQuery("select g from sec$Group g where g.name = ?1");
q.setView(new View(Group.class, false).addProperty("name"));
q.setParameter(1, "Company");
// noinspection unchecked
List<Group> groups = q.getResultList();
if (!groups.isEmpty()) {
Group defaultGroup = groups.get(0);
// load 'admin' user
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 user = list.get(0);
// set the default value
user.setGroup(defaultGroup);
}
}
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class UnfetchedAttributeTest method testGet.
@Test
public void testGet() 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);
// lazy fetch
user.getGroup();
}
tx.commit();
} finally {
tx.end();
}
user = reserialize(user);
assertNotNull(user);
assertNotNull(user.getUserRoles());
user.getUserRoles().size();
assertNotNull(user.getGroup());
}
Aggregations