use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class SoftDeleteTest method testReferenceToDeletedEntityThroughOneToMany.
@Test
public void testReferenceToDeletedEntityThroughOneToMany() throws Exception {
FetchPlan userRoleView = new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("deleteTs"));
FetchPlan userView = new View(User.class).addProperty("userRoles", userRoleView);
Role deleted = persistence.callInTransaction((em) -> em.find(Role.class, role3Id));
assertNull(deleted);
UserRole userRole = persistence.callInTransaction((em) -> em.find(UserRole.class, userRole3Id, userRoleView));
assertNotNull(userRole.getRole());
assertEquals(role3Id, userRole.getRole().getId());
assertTrue(userRole.getRole().isDeleted());
User user = persistence.callInTransaction((em) -> em.find(User.class, user1Id, userView));
assertEquals(role3Id, user.getUserRoles().iterator().next().getRole().getId());
Assertions.assertTrue(user.getUserRoles().iterator().next().getRole().isDeleted());
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToOne.
@Test
public void testOneToOne() {
System.out.println("===================== BEGIN testOneToOne =====================");
// test fetchMode = AUTO
System.out.println("===================== BEGIN testOneToOne fetchMode = AUTO =====================");
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"));
SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
assertNotNull(oneToOneA);
assertNotNull(oneToOneA.getB());
assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
tx.commit();
} finally {
tx.end();
}
// test fetchMode = BATCH
System.out.println("===================== BEGIN testOneToOneBy fetchMode = BATCH =====================");
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"), FetchMode.BATCH);
SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
assertNotNull(oneToOneA);
assertNotNull(oneToOneA.getB());
assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
tx.commit();
} finally {
tx.end();
}
// test fetchMode = UNDEFINED
System.out.println("===================== BEGIN testOneToOneBy fetchMode = UNDEFINED =====================");
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"), FetchMode.UNDEFINED);
SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
assertNotNull(oneToOneA);
assertNotNull(oneToOneA.getB());
assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOne =====================");
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToMany_CleanupMode.
@Test
public void testOneToMany_CleanupMode() {
System.out.println("===================== BEGIN testOneToMany_CleanupMode =====================");
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
em.setSoftDeletion(false);
FetchPlan view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("userRoles", new View(UserRole.class, "testView").addProperty("role", new View(Role.class, "testView").addProperty("name")));
User user = em.find(User.class, userId, view);
List<UserRole> userRoles = user.getUserRoles();
assertEquals(2, userRoles.size());
for (UserRole ur : userRoles) {
assertNotNull(ur.getRole());
}
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToMany_CleanupMode =====================");
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToOneMappedByQuery.
@Test
public void testOneToOneMappedByQuery() {
System.out.println("===================== BEGIN testOneToOneMappedByQuery =====================");
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
SoftDeleteOneToOneA oneToOneA3 = metadata.create(SoftDeleteOneToOneA.class);
oneToOneA3.setName("oneToOneA3");
oneToOneA3.setB(em.find(SoftDeleteOneToOneB.class, oneToOneB1Id));
em.persist(oneToOneA3);
oneToOneA3Id = oneToOneA3.getId();
tx.commit();
} finally {
tx.end();
}
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
FetchPlan view = new View(SoftDeleteOneToOneB.class, "testView").addProperty("name").addProperty("a", new View(SoftDeleteOneToOneA.class, "testView").addProperty("name"));
List<SoftDeleteOneToOneB> r = em.createQuery("select b from test$SoftDeleteOneToOneB b where b.name = :name", SoftDeleteOneToOneB.class).setParameter("name", "oneToOneB1").setView(view).getResultList();
assertEquals(1, r.size());
assertEquals(r.get(0).getA().getId(), oneToOneA3Id);
assertEquals(r.get(0).getA().getName(), "oneToOneA3");
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOneMappedByQuery =====================");
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class NativeQueryTest method testNativeQueryWithDataManager.
@Test
@Disabled
public void testNativeQueryWithDataManager() {
// https://www.cuba-platform.ru/discuss/t/problema-s-podgruzkoj-obekta-po-view-dobavlennogo-v-toj-zhe-tranzakiczii/5123
// given:
Customer customer = dataManager.create(Customer.class);
customer.setName("c1");
Order order = dataManager.create(Order.class);
order.setNumber("c1");
order.setCustomer(customer);
dataManager.save(customer, order);
storeAwareLocator.getTransactionTemplate(Stores.MAIN).executeWithoutResult(tx -> {
EntityManager entityManager = storeAwareLocator.getEntityManager(Stores.MAIN);
entityManager.createNativeQuery("select id from SALES_ORDER where ID = ?1", Order.class).setParameter(1, order.getId()).getSingleResult();
Order reloadedOrder = dataManager.load(Order.class).id(order.getId()).fetchPlan(builder -> {
builder.add("number").add("customer", FetchPlan.LOCAL);
}).one();
assertEquals("c1", reloadedOrder.getNumber());
assertEquals(customer, reloadedOrder.getCustomer());
});
}
Aggregations