use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testCascadeMergeManaged.
// Test cascade merge on a managed entity
// Test for GF#1139 - Cascade doesn't work when merging managed entity
public void testCascadeMergeManaged() {
// setup
Project p1 = new Project();
p1.setName("Project1");
Project p2 = new Project();
p1.setName("Project2");
Employee e1 = new Employee();
e1.setFirstName("Employee1");
Employee e2 = new Employee();
e2.setFirstName("Employee2");
EntityManager em = createEntityManager();
beginTransaction(em);
try {
em.persist(p1);
em.persist(p2);
em.persist(e1);
em.persist(e2);
commitTransaction(em);
} catch (RuntimeException re) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw re;
}
closeEntityManager(em);
// end of setup
// p1,p2,e1,e2 are detached
em = createEntityManager();
beginTransaction(em);
try {
Project mp1 = em.merge(p1);
assertTrue(em.contains(mp1));
assertTrue("Managed instance and detached instance must not be same", mp1 != p1);
// p1 -> e1 (one-to-one)
mp1.setTeamLeader(e1);
// merge again - trigger cascade merge
mp1 = em.merge(mp1);
Employee me1 = mp1.getTeamLeader();
assertTrue("Cascade merge failed", em.contains(me1));
assertTrue("Managed instance and detached instance must not be same", me1 != e1);
// e1 -> e2 (one-to-many)
me1.addManagedEmployee(e2);
// merge again - trigger cascade merge
me1 = em.merge(me1);
Employee me2 = me1.getManagedEmployees().iterator().next();
assertTrue("Cascade merge failed", em.contains(me2));
assertTrue("Managed instance and detached instance must not be same", me2 != e2);
// e2 -> p2 (many-to-many)
me2.addProject(p2);
p2.addTeamMember(me2);
// merge again - trigger cascade merge
me2 = em.merge(me2);
Project mp2 = me2.getProjects().iterator().next();
assertTrue("Cascade merge failed", em.contains(mp2));
assertTrue("Managed instance and detached instance must not be same", mp2 != p2);
commitTransaction(em);
} catch (RuntimeException re) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw re;
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjectsWithName.
protected void internalTestUpdateAllProjectsWithName(Class<?> cls) {
if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
getServerSession().logMessage("Test testUpdateAll*ProjectsWithName skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
return;
}
String className = Helper.getShortClassName(cls);
String name = "testUpdateAllProjects";
String newName = "testUpdateAllProjectsNEW";
boolean ok = false;
try {
// setup
// make sure no projects with the specified names exist
deleteProjectsWithName(name);
deleteProjectsWithName(newName);
// populate Projects
EntityManager em = createEntityManager();
beginTransaction(em);
try {
createProjectsWithName(name, null, em);
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
// test
em = createEntityManager();
beginTransaction(em);
try {
em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "' WHERE p.name = '" + name + "'").executeUpdate();
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
// verify
em = createEntityManager();
String errorMsg = "";
try {
List projects = em.createQuery("SELECT OBJECT(p) FROM Project p WHERE p.name = '" + newName + "' OR p.name = '" + name + "'").getResultList();
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
String readName = p.getName();
if (cls.isInstance(p)) {
if (!readName.equals(newName)) {
errorMsg = errorMsg + "haven't updated name: " + p + "; ";
}
} else {
if (readName.equals(newName)) {
errorMsg = errorMsg + "have updated name: " + p + "; ";
}
}
}
} catch (RuntimeException ex) {
if (usesSOP() && !isSOPRecoverable()) {
if (ex instanceof PersistenceException) {
if (ex.getCause() instanceof QueryException && ((QueryException) ex.getCause()).getErrorCode() == QueryException.SOP_OBJECT_IS_NOT_FOUND) {
// getResultList is expected to fail because SOP field is set to null after bulk update
} else {
fail("Wrong cause of PersistenceException: " + ex.getCause());
}
} else {
fail("PersistenceException was expected");
}
} else {
throw ex;
}
} finally {
closeEntityManager(em);
}
if (errorMsg.length() > 0) {
fail(errorMsg);
} else {
ok = true;
}
} finally {
// make sure no projects with the specified names left
try {
deleteProjectsWithName(name);
deleteProjectsWithName(newName);
} catch (RuntimeException ex) {
// eat clean-up exception in case the test failed
if (ok) {
throw ex;
}
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityMethodPostLoadTransactionTest method test.
@Override
public void test() throws Exception {
beginTransaction();
// New object, count starts at 0.
m_beforeEvent = 0;
Project project = getEntityManager().find(Project.class, m_project.getId());
m_afterEvent = project.post_load_count;
this.rollbackTransaction();
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityMethodPostUpdateTest method test.
@Override
public void test() throws Exception {
// Loading a new object to update, count starts at 0.
m_beforeEvent = 0;
Project project = updateProject();
m_afterEvent = project.post_update_count;
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Project in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastInSelect.
public void testDowncastInSelect() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
SmallProject sp = new SmallProject();
sp.setName("sp1");
em.persist(sp);
em.flush();
clearCache();
em.clear();
// this would work in the past if TYPE was added to the where clause
// Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = qb.createQuery(Project.class);
Root<Project> root = cq.from(Project.class);
Root largeProjectRoot = qb.treat(root, LargeProject.class);
cq.select(largeProjectRoot.get("budget"));
List resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations