use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class FetchGroupTrackerWeavingTests method verifyCheckFetchedForSetWithFetchGroup_OneToMany.
@Test
public void verifyCheckFetchedForSetWithFetchGroup_OneToMany() {
Employee emp = new Employee();
TestFetchGroup fg = new TestFetchGroup();
((FetchGroupTracker) emp)._persistence_setFetchGroup(fg);
assertNull(this.checkAttribute);
assertNull(this.checkForSetAttribute);
emp.setPhoneNumbers(new ArrayList<PhoneNumber>());
assertNull(this.checkAttribute);
assertNotNull(this.checkForSetAttribute);
assertEquals("phoneNumbers", this.checkForSetAttribute);
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class NestedDefaultFetchGroupTests method allPhone.
@Test
public void allPhone() {
EntityManager em = createEntityManager("fieldaccess");
List<PhoneNumber> allPhones = em.createQuery("SELECT p FROM PhoneNumber p", PhoneNumber.class).getResultList();
for (PhoneNumber phone : allPhones) {
assertFetched(phone, defaultPhoneFG);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class NestedDefaultFetchGroupTests method internalFindMinEmployee.
void internalFindMinEmployee(boolean loadAddress, boolean loadPhones, boolean useLoadGroup) {
EntityManager em = createEntityManager("fieldaccess");
beginTransaction(em);
int minId = minEmployeeIdWithAddressAndPhones(em);
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
boolean load = false;
boolean originalLoad = false;
if (!useLoadGroup) {
assertTrue(loadAddress == loadPhones);
load = loadAddress;
originalLoad = defaultEmployeeFG.shouldLoad();
if (load != originalLoad) {
defaultEmployeeFG.setShouldLoad(load);
}
}
try {
Employee emp;
if (useLoadGroup) {
LoadGroup lg = defaultEmployeeFG.toLoadGroup();
if (!loadAddress) {
lg.removeAttribute("address");
}
if (!loadPhones) {
lg.removeAttribute("phoneNumbers");
}
HashMap<String, Object> hints = new HashMap(1);
hints.put(QueryHints.LOAD_GROUP, lg);
emp = em.find(Employee.class, minId, hints);
} else {
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) employeeDescriptor.getMappingForAttributeName("address").getAttributeValueFromObject(emp)).isInstantiated();
assertTrue(loadAddress == addressInstantiated);
boolean phonesInstantiated = ((IndirectCollection) employeeDescriptor.getMappingForAttributeName("phoneNumbers").getAttributeValueFromObject(emp)).isInstantiated();
assertTrue(loadPhones == phonesInstantiated);
emp.getAddress();
emp.getPhoneNumbers().size();
assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
assertFetched(emp, defaultEmployeeFG);
assertFetchedAttribute(emp, "address");
assertFetchedAttribute(emp, "phoneNumbers");
// Check Address
FetchGroup fgAddress = defaultEmployeeFG.getGroup("address");
assertFetched(emp.getAddress(), fgAddress);
// Check phones
FetchGroup fgPhones = defaultEmployeeFG.getGroup("phoneNumbers");
for (PhoneNumber phone : emp.getPhoneNumbers()) {
assertFetched(phone, fgPhones);
}
} finally {
if (!useLoadGroup) {
if (load != originalLoad) {
defaultEmployeeFG.setShouldLoad(originalLoad);
}
}
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method dynamicFetchGroup_EmployeeAddressNullPhone.
@Test
public void dynamicFetchGroup_EmployeeAddressNullPhone() throws Exception {
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.gender = :GENDER");
query.setParameter("GENDER", Gender.Male);
// Define the fields to be fetched on Employee
FetchGroup empGroup = new FetchGroup();
empGroup.addAttribute("firstName");
empGroup.addAttribute("lastName");
empGroup.addAttribute("address");
empGroup.addAttribute("address.city");
empGroup.addAttribute("address.postalCode");
// empGroup.addAttribute("phoneNumbers").setUseDefaultFetchGroup(false);
FetchGroup fullPhone = this.phoneDescriptor.getFetchGroupManager().createFullFetchGroup();
// to preclude Employee from being loaded by phoneNumber.owner add it to the fetch group
fullPhone.addAttribute("owner.id");
empGroup.addAttribute("phoneNumbers", fullPhone);
// Configure the dynamic FetchGroup
query.setHint(QueryHints.FETCH_GROUP, empGroup);
List<Employee> emps = query.getResultList();
assertNotNull(emps);
for (Employee emp : emps) {
FetchGroupTracker tracker = (FetchGroupTracker) emp;
assertNotNull(tracker._persistence_getFetchGroup());
// Verify specified fields plus mandatory ones are loaded
assertTrue(tracker._persistence_isAttributeFetched("id"));
assertTrue(tracker._persistence_isAttributeFetched("firstName"));
assertTrue(tracker._persistence_isAttributeFetched("lastName"));
assertTrue(tracker._persistence_isAttributeFetched("version"));
// Verify the other fields are not loaded
assertFalse(tracker._persistence_isAttributeFetched("salary"));
assertFalse(tracker._persistence_isAttributeFetched("startTime"));
assertFalse(tracker._persistence_isAttributeFetched("endTime"));
// Force the loading of lazy fields and verify
emp.getSalary();
assertTrue(tracker._persistence_isAttributeFetched("salary"));
assertTrue(tracker._persistence_isAttributeFetched("startTime"));
assertTrue(tracker._persistence_isAttributeFetched("endTime"));
// Now we'll check the address uses the provided dynamic fetch-group
FetchGroupTracker addrTracker = (FetchGroupTracker) emp.getAddress();
assertNotNull("Address does not have a FetchGroup", addrTracker._persistence_getFetchGroup());
assertTrue(addrTracker._persistence_isAttributeFetched("city"));
assertTrue(addrTracker._persistence_isAttributeFetched("postalCode"));
assertFalse(addrTracker._persistence_isAttributeFetched("street"));
assertFalse(addrTracker._persistence_isAttributeFetched("country"));
// Now we'll check the phoneNumbers use of the default fetch group
for (PhoneNumber phone : emp.getPhoneNumbers()) {
FetchGroupTracker phoneTracker = (FetchGroupTracker) phone;
assertNull("PhoneNumber has a FetchGroup", phoneTracker._persistence_getFetchGroup());
}
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method internal_dynamicFetchGroup_EmployeeAddressEmptyPhone.
void internal_dynamicFetchGroup_EmployeeAddressEmptyPhone(boolean shouldLoad) {
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.gender = :GENDER");
query.setParameter("GENDER", Gender.Male);
// Define the fields to be fetched on Employee
FetchGroup fg = new FetchGroup();
fg.addAttribute("firstName");
fg.addAttribute("lastName");
fg.addAttribute("address.city");
fg.addAttribute("address.postalCode");
// to preclude Employee from being loaded by phoneNumber.owner add it to the fetch group
FetchGroup ownerId = new FetchGroup();
ownerId.addAttribute("owner.id");
fg.addAttribute("phoneNumbers", ownerId);
if (shouldLoad) {
fg.setShouldLoad(true);
}
// Configure the dynamic FetchGroup
query.setHint(QueryHints.FETCH_GROUP, fg);
List<Employee> emps = query.getResultList();
assertNotNull(emps);
assertEquals(1 + (shouldLoad ? 0 : (emps.size() * 2)), getQuerySQLTracker(em).getTotalSQLSELECTCalls());
for (Employee emp : emps) {
FetchGroupTracker tracker = (FetchGroupTracker) emp;
assertNotNull(tracker._persistence_getFetchGroup());
// Verify specified fields plus mandatory ones are loaded
assertTrue(tracker._persistence_isAttributeFetched("id"));
assertTrue(tracker._persistence_isAttributeFetched("firstName"));
assertTrue(tracker._persistence_isAttributeFetched("lastName"));
assertTrue(tracker._persistence_isAttributeFetched("version"));
// Verify the other fields are not loaded
assertFalse(tracker._persistence_isAttributeFetched("salary"));
assertFalse(tracker._persistence_isAttributeFetched("startTime"));
assertFalse(tracker._persistence_isAttributeFetched("endTime"));
// Force the loading of lazy fields and verify
emp.getSalary();
assertTrue(tracker._persistence_isAttributeFetched("salary"));
assertTrue(tracker._persistence_isAttributeFetched("startTime"));
assertTrue(tracker._persistence_isAttributeFetched("endTime"));
// Now we'll check the address uses the provided dynamic fetch-group
FetchGroupTracker addrTracker = (FetchGroupTracker) emp.getAddress();
assertNotNull("Address does not have a FetchGroup", addrTracker._persistence_getFetchGroup());
assertTrue(addrTracker._persistence_isAttributeFetched("city"));
assertTrue(addrTracker._persistence_isAttributeFetched("postalCode"));
assertFalse(addrTracker._persistence_isAttributeFetched("street"));
assertFalse(addrTracker._persistence_isAttributeFetched("country"));
// Now we'll check the phoneNumbers use of the default fetch group
for (PhoneNumber phone : emp.getPhoneNumbers()) {
FetchGroupTracker phoneTracker = (FetchGroupTracker) phone;
assertNotNull("PhoneNumber does not have a FetchGroup", phoneTracker._persistence_getFetchGroup());
assertFalse(phoneTracker._persistence_isAttributeFetched("number"));
assertFalse(phoneTracker._persistence_isAttributeFetched("areaCode"));
phone.getNumber();
assertTrue(phoneTracker._persistence_isAttributeFetched("number"));
assertTrue(phoneTracker._persistence_isAttributeFetched("areaCode"));
}
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations