use of com.haulmont.cuba.core.model.SoftDeleteOneToOneB in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToOneMappedByLazy.
@Test
public void testOneToOneMappedByLazy() {
System.out.println("===================== BEGIN testOneToOneMappedByLazy =====================");
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB1Id);
assertNotNull(oneToOneB);
assertNull(oneToOneB.getA());
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOneMappedByLazy =====================");
}
use of com.haulmont.cuba.core.model.SoftDeleteOneToOneB in project jmix by jmix-framework.
the class SoftDeleteTest method setUp.
@BeforeEach
public void setUp() throws Exception {
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Group group = new Group();
groupId = group.getId();
group.setName("testGroup");
em.persist(group);
User user = new User();
userId = user.getId();
user.setName("testUser");
user.setLogin("testLogin");
user.setGroup(group);
em.persist(user);
User user1 = new User();
user1Id = user1.getId();
user1.setName("testUser1");
user1.setLogin("testLogin1");
user1.setGroup(group);
em.persist(user1);
User user2 = new User();
user2Id = user2.getId();
user2.setName("testUser2");
user2.setLogin("testLogin2");
user2.setGroup(group);
em.persist(user2);
Role role1 = new Role();
role1Id = role1.getId();
role1.setName("role1");
em.persist(role1);
UserRole userRole1 = new UserRole();
userRole1Id = userRole1.getId();
userRole1.setUser(user);
userRole1.setRole(role1);
em.persist(userRole1);
Role role2 = new Role();
role2Id = role2.getId();
role2.setName("roleToBeDeleted");
em.persist(role2);
Role role3 = new Role();
role3Id = role3.getId();
role3.setName("roleToBeDeleted3");
em.persist(role3);
Role role4 = new Role();
role4Id = role4.getId();
role4.setName("role4");
em.persist(role4);
UserRole userRole2 = new UserRole();
userRole2Id = userRole2.getId();
userRole2.setUser(user);
userRole2.setRole(role2);
em.persist(userRole2);
UserRole userRole3 = new UserRole();
userRole3Id = userRole3.getId();
userRole3.setUser(user1);
userRole3.setRole(role3);
em.persist(userRole3);
UserRole userRole4 = new UserRole();
userRole4Id = userRole4.getId();
userRole4.setUser(user2);
userRole4.setRole(role4);
em.persist(userRole4);
SoftDeleteOneToOneB oneToOneB1 = metadata.create(SoftDeleteOneToOneB.class);
oneToOneB1.setName("oneToOneB1");
em.persist(oneToOneB1);
oneToOneB1Id = oneToOneB1.getId();
SoftDeleteOneToOneB oneToOneB2 = metadata.create(SoftDeleteOneToOneB.class);
oneToOneB2.setName("oneToOneB2");
em.persist(oneToOneB2);
oneToOneB2Id = oneToOneB2.getId();
SoftDeleteOneToOneA oneToOneA1 = metadata.create(SoftDeleteOneToOneA.class);
oneToOneA1.setName("oneToOneA1");
oneToOneA1.setB(oneToOneB1);
em.persist(oneToOneA1);
oneToOneA1Id = oneToOneA1.getId();
SoftDeleteOneToOneA oneToOneA2 = metadata.create(SoftDeleteOneToOneA.class);
oneToOneA2.setName("oneToOneA2");
oneToOneA2.setB(oneToOneB2);
em.persist(oneToOneA2);
oneToOneA2Id = oneToOneA2.getId();
Group group1 = new Group();
group1Id = group1.getId();
group1.setName("testGroup1");
em.persist(group1);
GroupHierarchy groupHierarchy = new GroupHierarchy();
groupHierarchyId = groupHierarchy.getId();
groupHierarchy.setGroup(group1);
groupHierarchy.setParent(group1);
groupHierarchy.setLevel(1);
em.persist(groupHierarchy);
Constraint constraint1 = new Constraint();
constraint1Id = constraint1.getId();
constraint1.setCode("constraint1");
constraint1.setEntityName("test$Constraint");
constraint1.setGroup(group1);
em.persist(constraint1);
Constraint constraint2 = new Constraint();
constraint2Id = constraint2.getId();
constraint2.setCode("constraint2");
constraint2.setEntityName("test$Constraint");
constraint2.setGroup(group1);
em.persist(constraint2);
tx.commitRetaining();
em = persistence.getEntityManager();
UserRole ur = em.find(UserRole.class, userRole2Id);
em.remove(ur);
Role r = em.find(Role.class, role2Id);
em.remove(r);
SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA1Id);
em.remove(oneToOneA);
SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB2Id);
em.remove(oneToOneB);
Constraint constraint = em.find(Constraint.class, constraint2Id);
em.remove(constraint);
// remove from db to prevent cascade delete user role
JdbcTemplate jdbcTemplate = new JdbcTemplate(persistence.getDataSource());
jdbcTemplate.update("update TEST_ROLE set DELETE_TS = CREATE_TS, DELETED_BY = CREATED_BY where name = 'roleToBeDeleted3'");
tx.commit();
} finally {
tx.end();
}
JdbcTemplate jdbcTemplate = new JdbcTemplate(persistence.getDataSource());
jdbcTemplate.update("update TEST_USER set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", user2Id.toString());
}
use of com.haulmont.cuba.core.model.SoftDeleteOneToOneB in project jmix by jmix-framework.
the class SoftDeleteTest method testOneToOneMappedBy.
@Test
public void testOneToOneMappedBy() {
System.out.println("===================== BEGIN testOneToOneMappedBy =====================");
// test fetchMode = AUTO
System.out.println("===================== BEGIN testOneToOneMappedBy fetchMode = AUTO =====================");
Transaction 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"));
SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB1Id, view);
assertNotNull(oneToOneB);
assertNull(oneToOneB.getA());
tx.commit();
} finally {
tx.end();
}
// test fetchMode = BATCH
System.out.println("===================== BEGIN testOneToOneMappedBy fetchMode = BATCH =====================");
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"), FetchMode.BATCH);
SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB1Id, view);
assertNotNull(oneToOneB);
assertNull(oneToOneB.getA());
tx.commit();
} finally {
tx.end();
}
// test fetchMode = UNDEFINED
System.out.println("===================== BEGIN testOneToOneMappedBy fetchMode = UNDEFINED =====================");
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"), FetchMode.UNDEFINED);
SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB1Id, view);
assertNotNull(oneToOneB);
assertNull(oneToOneB.getA());
tx.commit();
} finally {
tx.end();
}
System.out.println("===================== END testOneToOneMappedBy =====================");
}
use of com.haulmont.cuba.core.model.SoftDeleteOneToOneB 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 =====================");
}
Aggregations