use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testSetFieldForPropertyAccessWithNewEM.
/**
* Bug 801
* Test to ensure when property access is used and the underlying variable is changed the change
* is correctly reflected in the database
*
* In this test we test making the change when an existing object is read into a new EM
*/
public void testSetFieldForPropertyAccessWithNewEM() {
EntityManager em = createEntityManager();
Employee employee = new Employee();
employee.setFirstName("Andy");
employee.setLastName("Dufresne");
Employee manager = new Employee();
manager.setFirstName("Bobby");
manager.setLastName("Dufresne");
employee.setManager(manager);
Address address = new Address();
address.setCity("Shawshank");
employee.setAddress(address);
beginTransaction(em);
em.persist(employee);
commitTransaction(em);
int id = employee.getId();
int addressId = address.getID();
int managerId = manager.getId();
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, id);
employee.getAddress();
employee.getManager();
address = new Address();
address.setCity("Metropolis");
employee.setAddress(address);
manager = new Employee();
manager.setFirstName("Metro");
manager.setLastName("Dufresne");
employee.setManagerField(manager);
try {
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw e;
}
clearCache();
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, id);
address = employee.getAddress();
manager = employee.getManager();
assertTrue("The address was not persisted.", employee.getAddress() != null);
assertTrue("The address was not correctly persisted.", employee.getAddress().getCity().equals("Metropolis"));
assertTrue("The manager was not persisted.", employee.getManager() != null);
assertTrue("The manager was not correctly persisted.", employee.getManager().getFirstName().equals("Metro"));
Address initialAddress = em.find(Address.class, addressId);
Employee initialManager = em.find(Employee.class, managerId);
employee.setAddress((Address) null);
employee.setManager(null);
em.remove(address);
em.remove(employee);
em.remove(manager);
em.remove(initialAddress);
em.remove(initialManager);
commitTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNoPersistOnCommitProperties.
// Test Not using the persist operation on commit.
public void testNoPersistOnCommitProperties() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
Map properties = new HashMap();
properties.putAll(JUnitTestCaseHelper.getDatabaseProperties());
properties.put(EntityManagerProperties.PERSISTENCE_CONTEXT_COMMIT_WITHOUT_PERSIST_RULES, "true");
EntityManager em = createEntityManager(properties);
beginTransaction(em);
Employee employee = new Employee();
employee.setLastName("SomeName");
Address addr = new Address();
addr.setCity("Douglas");
try {
em.persist(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager(properties);
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
employee.setAddress(addr);
addr.getEmployees().add(employee);
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager(properties);
clearCache();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
employee.getAddress().setCountry("country");
employee.getAddress().getEmployees().size();
employee.setAddress((Address) null);
em.remove(employee);
commitTransaction(em);
em = createEntityManager(properties);
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
assertNull("Employee Not Deleted", employee);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
} finally {
try {
em = createEntityManager();
clearCache();
beginTransaction(em);
em.remove(em.find(Address.class, addr.getID()));
commitTransaction(em);
} catch (RuntimeException ex) {
// ignore
}
}
}
use of org.eclipse.persistence.testing.models.jpa.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();
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.advanced.Address in project eclipselink by eclipse-ee4j.
the class FetchGroupTrackerWeavingTests method verifyCheckFetchedWithFetchGroup_OneToOne.
@Test
public void verifyCheckFetchedWithFetchGroup_OneToOne() {
Employee emp = new Employee();
TestFetchGroup fg = new TestFetchGroup();
((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
assertNull(this.checkAttribute);
assertNull(this.checkForSetAttribute);
Address addr = emp.getAddress();
assertNull(addr);
assertNull(this.checkForSetAttribute);
assertNotNull(this.checkAttribute);
assertEquals("address", this.checkAttribute);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class FetchGroupTrackerWeavingTests method verifyCheckFetchedForSetWithFetchGroup_OneToOne.
@Test
public void verifyCheckFetchedForSetWithFetchGroup_OneToOne() {
Employee emp = new Employee();
TestFetchGroup fg = new TestFetchGroup();
((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
assertNull(this.checkAttribute);
assertNull(this.checkForSetAttribute);
emp.setAddress(new Address());
assertNull(this.checkAttribute);
assertNotNull(this.checkForSetAttribute);
assertEquals("address", this.checkForSetAttribute);
}
Aggregations