use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department in project eclipselink by eclipse-ee4j.
the class AdvancedCompositePKJunitTest method testGetIdentifier.
public void testGetIdentifier() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = createEntityManager();
beginTransaction(em);
try {
DepartmentPK pk = new DepartmentPK("DEPT B", "ROLE B", "LOCATION B");
Department department = new Department();
department.setName("DEPT B");
department.setRole("ROLE B");
department.setLocation("LOCATION B");
em.persist(department);
em.flush();
PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
assertTrue("Got an incorrect id from persistenceUtil.getIdentifier()", pk.equals(util.getIdentifier(department)));
} finally {
rollbackTransaction(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department in project eclipselink by eclipse-ee4j.
the class AdvancedCompositePKJunitTest method testCreateDepartment.
public void testCreateDepartment() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
// make sure the department is not left from the previous test run
em.createQuery("DELETE FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'").executeUpdate();
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
clearCache();
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
try {
Department department = new Department();
department.setName("DEPT A");
department.setRole("ROLE A");
department.setLocation("LOCATION A");
Competency competency = new Competency();
competency.description = "Manage groups";
competency.rating = 9;
department.addCompetency(competency);
em.persist(department);
commitTransaction(em);
m_departmentPK = department.getPK();
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department in project eclipselink by eclipse-ee4j.
the class AdvancedCompositePKJunitTest method testAnyAndAll.
// bug gf672 - JPQL Select query with IN/ANY in WHERE clause and subselect fails.
public void testAnyAndAll() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Scientist> cq = qb.createQuery(Scientist.class);
Subquery sq = cq.subquery(Scientist.class);
Root<Scientist> from_scientist = cq.from(Scientist.class);
cq.where(qb.equal(from_scientist, qb.any(sq)));
Query query1 = em.createQuery(cq);
List<Scientist> results1 = query1.getResultList();
// Query query2 = em.createQuery("SELECT s FROM Scientist s WHERE s = ALL (SELECT s2 FROM Scientist s2)");
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Scientist.class);
sq = cq.subquery(Scientist.class);
from_scientist = cq.from(Scientist.class);
cq.where(qb.equal(from_scientist, qb.all(sq)));
Query query2 = em.createQuery(cq);
List<Scientist> results2 = query2.getResultList();
// Query query3 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ALL (SELECT DISTINCT d FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A')");
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Scientist.class);
sq = cq.subquery(Department.class);
sq.distinct(true);
Root<Department> from_department = sq.from(Department.class);
sq.where(qb.and(qb.and(qb.equal(from_department.get("name"), "DEPT A"), qb.equal(from_department.get("role"), "ROLE A")), qb.equal(from_department.get("location"), "LOCATION A")));
from_scientist = cq.from(Scientist.class);
cq.where(qb.equal(from_scientist.get("department"), qb.all(sq)));
Query query3 = em.createQuery(cq);
List<Scientist> results3 = query3.getResultList();
// Query query4 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ANY (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G')");
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Scientist.class);
sq = cq.subquery(Department.class);
sq.distinct(true);
from_department = sq.from(Department.class);
Join c = from_department.join("scientists").join("cubicle");
sq.where(qb.equal(c.get("code"), "G"));
from_scientist = cq.from(Scientist.class);
cq.where(qb.equal(from_scientist.get("department"), qb.any(sq)));
Query query4 = em.createQuery(cq);
List<Scientist> results4 = query4.getResultList();
// control queries
// Query controlQuery1 = em.createQuery("SELECT s FROM Scientist s");
Query controlQuery1 = em.createQuery(em.getCriteriaBuilder().createQuery(Scientist.class));
List<Scientist> controlResults1 = controlQuery1.getResultList();
List<Scientist> controlResults2;
if (controlResults1.size() == 1) {
controlResults2 = controlResults1;
} else {
controlResults2 = new ArrayList<Scientist>();
}
// Query controlQuery3 = em.createQuery("SELECT s FROM Scientist s JOIN s.department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'");
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Scientist.class);
from_scientist = cq.from(Scientist.class);
Join d = from_scientist.join("department");
cq.where(qb.and(qb.and(qb.equal(d.get("name"), "DEPT A"), qb.equal(d.get("role"), "ROLE A")), qb.equal(d.get("location"), "LOCATION A")));
Query controlQuery3 = em.createQuery(cq);
List<Scientist> controlResults3 = controlQuery3.getResultList();
// Query controlQuery4 = em.createQuery("SELECT s FROM Scientist s WHERE EXISTS (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G' AND d = s.department)");
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Scientist.class);
sq = cq.subquery(Department.class);
sq.distinct(true);
from_department = sq.from(Department.class);
from_scientist = cq.from(Scientist.class);
c = from_department.join("scientists").join("cubicle");
sq.where(qb.and(qb.equal(c.get("code"), "G"), qb.equal(from_department, from_scientist.get("department"))));
cq.where(qb.exists(sq));
Query controlQuery4 = em.createQuery(cq);
List<Scientist> controlResults4 = controlQuery4.getResultList();
// compare results - they should be the same
compareResults(results1, controlResults1, "query1");
compareResults(results2, controlResults2, "query2");
compareResults(results3, controlResults3, "query3");
compareResults(results4, controlResults4, "query4");
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department in project eclipselink by eclipse-ee4j.
the class AdvancedCompositePKJunitTest method testJoinColumnSharesPK.
public void testJoinColumnSharesPK() {
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = createEntityManager();
beginTransaction(em);
org.eclipse.persistence.descriptors.ClassDescriptor descriptor = getServerSession().getDescriptor(Office.class);
try {
Department department = new Department();
department.setName("DEPT B");
department.setRole("ROLE B");
department.setLocation("LOCATION B");
em.persist(department);
Office office = new Office();
office.setId(1);
office.setLocation("LOCATION B");
office.setDepartment(department);
em.persist(office);
department.getOffices().add(office);
em.flush();
clearCache();
office = em.find(Office.class, new OfficePK(1, "LOCATION B"));
department = em.find(Department.class, new DepartmentPK("DEPT B", "ROLE B", "LOCATION B"));
assertTrue("Office's department not properly persisted", office.getDepartment() != null);
assertTrue("Department's offices not properly persisted", department.getOffices().size() > 0);
} catch (Exception e) {
fail("Exception thrown while inserting an object with a read-only column in a foreign key." + e);
} finally {
rollbackTransaction(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department in project eclipselink by eclipse-ee4j.
the class AdvancedCompositePKJunitTest method testReadDepartment.
public void testReadDepartment() {
Department department = createEntityManager().find(Department.class, m_departmentPK);
assertTrue("Error on reading back the ordered department list.", department != null);
assertTrue("The number of scientists were incorrect.", department.getScientists().size() > 0);
}
Aggregations