use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testPESSIMISTIC_FORCE_INCREMENTLockOnNonVersionedEntity.
public void testPESSIMISTIC_FORCE_INCREMENTLockOnNonVersionedEntity() {
Department dept = null;
EntityManager em = createEntityManager();
try {
beginTransaction(em);
dept = new Department();
em.persist(dept);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
try {
beginTransaction(em);
dept = em.find(Department.class, dept.getId(), LockModeType.PESSIMISTIC_FORCE_INCREMENT);
rollbackTransaction(em);
fail("An Expected jakarta.persistence.PersistenceException was not thrown");
} catch (PersistenceException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class AdvancedJPAJunitTest method testMapBuildReferencesPKList.
public void testMapBuildReferencesPKList() {
EntityManager em = createEntityManager();
beginTransaction(em);
ClassDescriptor descriptor;
AbstractSession session = (AbstractSession) JpaHelper.getEntityManager(em).getActiveSession();
descriptor = session.getDescriptorForAlias("ADV_DEPT");
Department dept = (Department) em.createQuery("SELECT d FROM ADV_DEPT d WHERE d.equipment IS NOT EMPTY").getResultList().get(0);
ForeignReferenceMapping mapping = (ForeignReferenceMapping) descriptor.getMappingForAttributeName("equipment");
Object[] pks = mapping.buildReferencesPKList(dept, mapping.getAttributeValueFromObject(dept), session);
assertTrue("PK list is of incorrect size. pks.size: " + pks.length + " expected: " + (dept.getEquipment().size() * 2), pks.length == (dept.getEquipment().size() * 2));
Map<Integer, Equipment> equipments = (Map<Integer, Equipment>) mapping.valueFromPKList(pks, null, session);
assertTrue("ValueFromPKList returned list of different size from actual entity.", equipments.size() == dept.getEquipment().size());
for (Equipment equip : dept.getEquipment().values()) {
assertTrue("Equipment not found in ValueFromPKList list", equipments.containsKey(equip.getId()));
}
rollbackTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class AdvancedJPAJunitTest method testAddNewEquipmentToDepartment.
/**
* Tests adding objects to a 1-M mapping that uses a map.
*/
public void testAddNewEquipmentToDepartment() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
EJBQueryImpl query = (EJBQueryImpl) em.createNamedQuery("findAllSQLEquipment");
Collection<Equipment> equipment = query.getResultCollection();
if (equipment.isEmpty()) {
fail("No Equipment was found. testCreateNewEquipment should have created new equipment and should have run before this test.");
} else {
Department department = new Department();
department.setName("Department with equipment");
for (Equipment e : equipment) {
department.addEquipment(e);
}
em.persist(department);
deptId = department.getId();
commitTransaction(em);
}
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
// Re-throw exception to ensure stacktrace appears in test result.
throw e;
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method dynamicFetchGroup_ElementCollection.
@Test
public void dynamicFetchGroup_ElementCollection() {
EntityManager em = createEntityManager();
AttributeGroup compt = new AttributeGroup(null, Competency.class, true);
compt.addAttribute("description");
AttributeGroup fg = new AttributeGroup(null, org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department.class, true);
fg.addAttribute("competencies", compt);
clearCache();
Collection<org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department> results = em.createQuery("select d from Department d").setHint(QueryHints.FETCH_GROUP, fg.toFetchGroup()).getResultList();
for (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department dept : results) {
assertFalse("Collection fetched: scientists, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("scientists"));
assertFalse("Collection fetched: offices, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("offices"));
assertTrue("Collection not fetched: competencies, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("competencies"));
for (Competency embeded : dept.getCompetencies()) {
assertTrue("Element attribute not loaded: description, fg ignored", ((FetchGroupTracker) embeded)._persistence_isAttributeFetched("description"));
}
dept.getScientists().size();
assertTrue("Collection not fetched: scientists, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("scientists"));
assertTrue("Collection not fetched: offices, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("offices"));
assertTrue("Collection not fetched: competencies, fg ignored", ((FetchGroupTracker) dept)._persistence_isAttributeFetched("competencies"));
for (Competency embeded : dept.getCompetencies()) {
embeded.getRating();
assertTrue("Element attribute not loaded: description, fg ignored", ((FetchGroupTracker) embeded)._persistence_isAttributeFetched("description"));
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.
@Test
public void allNestedFetchGroupWithJoinFetch() {
EntityManager em = createEntityManager();
try {
beginTransaction(em);
// select employees who are neither managers nor team leaders
Query query = em.createQuery("SELECT e FROM Employee e WHERE NOT EXISTS(SELECT p.id FROM Project p WHERE p.teamLeader = e) AND NOT EXISTS(SELECT e2.id FROM Employee e2 WHERE e2.manager = e)");
FetchGroup employeeFG = new FetchGroup("employee");
employeeFG.addAttribute("lastName");
employeeFG.addAttribute("address.country");
employeeFG.addAttribute("address.city");
query.setHint(QueryHints.LEFT_FETCH, "e.address");
employeeFG.addAttribute("phoneNumbers");
query.setHint(QueryHints.LEFT_FETCH, "e.phoneNumbers");
employeeFG.addAttribute("projects.name");
employeeFG.addAttribute("projects.teamLeader.firstName");
// employeeFG.addAttribute("projects.teamLeader.address.street");
// employeeFG.addAttribute("projects.teamLeader.address.postalCode");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.owner");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.type");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.areaCode");
query.setHint(QueryHints.LEFT_FETCH, "e.projects.teamLeader.phoneNumbers");
employeeFG.addAttribute("manager.firstName");
// employeeFG.addAttribute("manager.address.street");
// employeeFG.addAttribute("manager.address.postalCode");
employeeFG.addAttribute("manager.phoneNumbers.owner");
employeeFG.addAttribute("manager.phoneNumbers.type");
employeeFG.addAttribute("manager.phoneNumbers.areaCode");
query.setHint(QueryHints.LEFT_FETCH, "e.manager.phoneNumbers");
// department attribute defined with JoinFetchType.OUTER
employeeFG.addAttribute("department.name");
query.setHint(QueryHints.FETCH_GROUP, employeeFG);
List<Employee> employees = query.getResultList();
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
for (Employee emp : employees) {
assertFetched(emp, employeeFG);
Address address = emp.getAddress();
if (address != null) {
assertFetched(address, employeeFG.getGroup("address"));
}
for (PhoneNumber phone : emp.getPhoneNumbers()) {
assertFetched(phone, defaultPhoneFG);
}
for (Project project : emp.getProjects()) {
assertFetched(project, employeeFG.getGroup("projects"));
Employee teamLeader = project.getTeamLeader();
if (teamLeader != null) {
assertFetched(teamLeader, employeeFG.getGroup("projects.teamLeader"));
for (PhoneNumber phone : teamLeader.getPhoneNumbers()) {
assertFetched(phone, employeeFG.getGroup("projects.teamLeader.phoneNumbers"));
}
}
}
Employee manager = emp.getManager();
if (manager != null) {
assertFetched(manager, employeeFG.getGroup("manager"));
for (PhoneNumber phone : manager.getPhoneNumbers()) {
assertFetched(phone, employeeFG.getGroup("manager.phoneNumbers"));
}
}
Department department = emp.getDepartment();
if (department != null) {
assertFetched(department, employeeFG.getGroup("department"));
}
}
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations