use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testSettingDetachedObject.
public void testSettingDetachedObject() {
EntityManager em = createEntityManager();
List<Address> results = em.createQuery("Select a from Address a").getResultList();
Department dept1 = new Department("Dept1");
Department dept2 = new Department("Dept2");
beginTransaction(em);
em.persist(dept1);
em.persist(dept2);
commitTransaction(em);
em.clear();
Employee emp = new Employee("Bob", "Bob");
beginTransaction(em);
em.persist(emp);
emp.setDepartment(dept1);
em.flush();
// <-- only the UOW is checked for existence
emp.setDepartment(dept2);
// <-- fails here
commitTransaction(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testWeaving.
/**
* Test that all of the classes in the advanced model were weaved as expected.
*/
public void testWeaving() {
// Only test if weaving was on, test runs without weaving must set this system property.
if (isWeavingEnabled()) {
internalTestWeaving(new Employee(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), true);
internalTestWeaving(new FormerEmployment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new Address(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new PhoneNumber(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new EmploymentPeriod(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
// field-locking
internalTestWeaving(new Buyer(), false, isWeavingForFetchGroupsEnabled(), false);
// field-locking
internalTestWeaving(new GoldBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
// field-locking
internalTestWeaving(new PlatinumBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
// eager 1-m
internalTestWeaving(new Department(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new Golfer(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new GolferPK(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new SmallProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new LargeProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new Man(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new Woman(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
// serialized
internalTestWeaving(new Vegetable(), false, isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new VegetablePK(), false, isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new WorldRank(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new Equipment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new EquipmentCode(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
internalTestWeaving(new PartnerLink(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testOneToManyDefaultJoinTableName.
// gf 1217 - Ensure join table defaults correctly when 'mappedby' not specified
public void testOneToManyDefaultJoinTableName() {
Department dept = new Department();
Employee manager = new Employee();
dept.addManager(manager);
EntityManager em = createEntityManager();
try {
beginTransaction(em);
em.persist(dept);
commitTransaction(em);
} catch (RuntimeException e) {
throw e;
} 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 AdvancedQueryTestSuite method testMapKeyBatchFetching.
/**
* Test join fetching of maps.
*/
public void testMapKeyBatchFetching() {
clearCache();
EntityManager em = createEntityManager();
beginTransaction(em);
// Count SQL.
QuerySQLTracker counter = new QuerySQLTracker(getServerSession());
try {
Query query = em.createQuery("Select d from ADV_DEPT d");
query.setHint(QueryHints.BATCH, "d.equipment");
query.setHint(QueryHints.BATCH, "d.employees");
query.setHint(QueryHints.BATCH, "d.managers");
List<Department> results = query.getResultList();
if (isWeavingEnabled() && counter.getSqlStatements().size() > 1) {
fail("Should have been 1 queries but was: " + counter.getSqlStatements().size());
}
int queries = 1;
for (Department department : results) {
queries = queries + department.getEquipment().size();
department.getEmployees().size();
department.getManagers().size();
}
assertTrue("No data to join.", queries > 1);
if (isWeavingEnabled() && counter.getSqlStatements().size() > 4) {
fail("Should have been " + 4 + " queries but was: " + counter.getSqlStatements().size());
}
clearCache();
for (Department department : results) {
verifyObject(department);
}
} finally {
rollbackTransaction(em);
closeEntityManager(em);
if (counter != null) {
counter.remove();
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Department in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testPESSIMISTIC_WRITELock.
public void testPESSIMISTIC_WRITELock() {
// Cannot create parallel entity managers in the server.
if (!isOnServer() && isSelectForUpateNoWaitSupported()) {
EntityManager em = createEntityManager();
Department dept = null;
try {
beginTransaction(em);
dept = new Department();
dept.setName("Pessimistic Department");
em.persist(dept);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
Exception pessimisticLockException = null;
try {
beginTransaction(em);
dept = em.find(Department.class, dept.getId());
em.lock(dept, LockModeType.PESSIMISTIC_WRITE);
dept.setName("New Pessimistic Department");
EntityManager em2 = createEntityManager();
try {
beginTransaction(em2);
Department dept2 = em2.find(Department.class, dept.getId());
HashMap properties = new HashMap();
// According to the spec a 0 indicates a NOWAIT clause.
properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 0);
em2.lock(dept2, LockModeType.PESSIMISTIC_WRITE, properties);
} catch (PersistenceException ex) {
if (ex instanceof jakarta.persistence.PessimisticLockException) {
pessimisticLockException = ex;
} else {
throw ex;
}
} finally {
rollbackTransaction(em2);
closeEntityManager(em2);
}
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw ex;
} finally {
closeEntityManager(em);
}
assertFalse("Proper exception not thrown when EntityManager.lock(object, PESSIMISTIC) is used.", pessimisticLockException == null);
}
}
Aggregations