use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address in project eclipselink by eclipse-ee4j.
the class NestedDefaultFetchGroupTests method allAddress.
/* void internalFindMinEmployee(boolean loadAddress, boolean loadPhones) {
EntityManager em = createEntityManager();
int minId = minEmployeeIdWithAddressAndPhones(em);
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
FetchGroup fg = employeeDescriptor.getFetchGroupManager().getDefaultFetchGroup();
FetchGroup phonesFg = fg.getGroup("phoneNumbers");
FetchGroup fgAddress = fg.getGroup("address");
boolean originalLoadAddress = fgAddress.shouldLoad();
if(originalLoadAddress != loadAddress) {
fgAddress.setShouldLoad(loadAddress);
}
FetchGroup fgPhones = fg.getGroup("phoneNumbers");
boolean originalLoadPhones = fgPhones.shouldLoad();
if(originalLoadPhones != loadPhones) {
fgPhones.setShouldLoad(loadPhones);
}
try {
Employee emp = em.find(Employee.class, minId);
assertNotNull(emp);
int nExpected = 2;
if(loadAddress) {
nExpected++;
}
if(loadPhones) {
nExpected++;
}
assertEquals(nExpected, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
boolean addressInstantiated = ((ValueHolderInterface)((ForeignReferenceMapping)employeeDescriptor.getMappingForAttributeName("address")).getAttributeValueFromObject(emp)).isInstantiated();
boolean phonesInstantiated = ((IndirectCollection)((ForeignReferenceMapping)employeeDescriptor.getMappingForAttributeName("phoneNumbers")).getAttributeValueFromObject(emp)).isInstantiated();
if(loadAddress) {
assertTrue(addressInstantiated);
}
if(loadPhones) {
assertTrue(phonesInstantiated);
}
emp.getAddress();
emp.getPhoneNumbers().size();
assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
assertFetched(emp, defaultEmployeeFG);
assertFetchedAttribute(emp, "address");
assertFetchedAttribute(emp, "phoneNumbers");
// Check Address
assertFetched(emp.getAddress(), fgAddress);
// Check phones
for (PhoneNumber phone: emp.getPhoneNumbers()) {
assertFetched(phone, fgPhones);
}
} finally {
if(originalLoadAddress != loadAddress) {
fgAddress.setShouldLoad(originalLoadAddress);
}
if(originalLoadPhones != loadPhones) {
fgPhones.setShouldLoad(originalLoadPhones);
}
}
}*/
@Test
public void allAddress() {
EntityManager em = createEntityManager("fieldaccess");
List<Address> allAddresses = em.createQuery("SELECT a FROM Address a", Address.class).getResultList();
for (Address address : allAddresses) {
assertNoFetchGroup(address);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.
@Test
public void allNestedFetchGroupWithJoinFetch() {
EntityManager em = createEntityManager("fieldaccess");
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);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address in project eclipselink by eclipse-ee4j.
the class SessionBeanTests method testSetup.
/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testSetup() throws Exception {
new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession("sessionbean"));
Employee bob = new Employee();
bob.setFirstName("Bob");
bob.setLastName("Jones");
bob.setAddress(new Address());
bob.setDepartment(new Department());
getEmployeeService().insert(bob);
Employee joe = new Employee();
joe.setFirstName("Joe");
joe.setLastName("Smith");
joe.setAddress(new Address());
joe.setDepartment(new Department());
getEmployeeService().insert(joe);
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testFindDeleteAllPersist.
// test for bug 4755392:
// AFTER DELETEALL OBJECT STILL DEEMED EXISTING
public void testFindDeleteAllPersist() {
if (getDatabaseSession().getPlatform().isSymfoware()) {
getDatabaseSession().logMessage("Test testFindDeleteAllPersist skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
return;
}
String firstName = "testFindDeleteAllPersist";
// create Employees
Employee empWithAddress = new Employee();
empWithAddress.setFirstName(firstName);
empWithAddress.setLastName("WithAddress");
empWithAddress.setAddress(new Address());
Employee empWithoutAddress = new Employee();
empWithoutAddress.setFirstName(firstName);
empWithoutAddress.setLastName("WithoutAddress");
EntityManager em = createEntityManager();
// make sure no Employee with the specified firstName exists.
beginTransaction(em);
try {
em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
// persist
beginTransaction(em);
try {
em.persist(empWithAddress);
em.persist(empWithoutAddress);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
// clear cache
clearCache();
em.clear();
// Find both to bring into the cache, delete empWithoutAddress.
// Because the address VH is not triggered both objects should be invalidated.
beginTransaction(em);
try {
Employee empWithAddressFound = em.find(Employee.class, empWithAddress.getId());
empWithAddressFound.toString();
Employee empWithoutAddressFound = em.find(Employee.class, empWithoutAddress.getId());
empWithoutAddressFound.toString();
int nDeleted = em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "' and e.address IS NULL").executeUpdate();
assertTrue(nDeleted > 0);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
// we can no longer rely on the query above to clear the Employee from the persistence context.
// Clearing the context to allow us to proceed.
em.clear();
// persist new empWithoutAddress - the one that has been deleted from the db.
beginTransaction(em);
try {
Employee newEmpWithoutAddress = new Employee();
newEmpWithoutAddress.setFirstName(firstName);
newEmpWithoutAddress.setLastName("newWithoutAddress");
newEmpWithoutAddress.setId(empWithoutAddress.getId());
em.persist(newEmpWithoutAddress);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
// persist new empWithAddress - the one still in the db.
beginTransaction(em);
try {
Employee newEmpWithAddress = new Employee();
newEmpWithAddress.setFirstName(firstName);
newEmpWithAddress.setLastName("newWithAddress");
newEmpWithAddress.setId(empWithAddress.getId());
em.persist(newEmpWithAddress);
fail("EntityExistsException was expected");
} catch (EntityExistsException ex) {
// "cant_persist_detatched_object" - ignore the expected exception
} finally {
rollbackTransaction(em);
}
// clean up
beginTransaction(em);
em.createQuery("DELETE FROM Employee e WHERE e.firstName = '" + firstName + "'").executeUpdate();
commitTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testCloneable.
// Test the clone method works correctly with lazy attributes.
public void testCloneable() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Employee employee = new Employee();
employee.setFirstName("Owen");
employee.setLastName("Hargreaves");
employee.getAddress();
Employee clone = employee.clone();
Address address = new Address();
address.setCity("Munich");
clone.setAddress(address);
clone.getAddress();
em.persist(clone);
if (employee.getAddress() == clone.getAddress()) {
fail("Changing clone address changed original.");
}
commitTransaction(em);
clearCache();
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, clone.getId());
clone = employee.clone();
address = new Address();
address.setCity("Not Munich");
clone.setAddress(address);
clone.getAddress();
if (employee.getAddress() == clone.getAddress()) {
fail("Changing clone address changed original.");
}
if (employee.getAddress() == null) {
fail("Changing clone address reset original to null.");
}
if (clone.getAddress() != address) {
fail("Changing clone did not work.");
}
em.remove(employee);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations