Search in sources :

Example 21 with Address

use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testPersistenceProperties.

// The test removed because we moved back to binding literals
// on platforms other than DB2 and Derby
/*    public void testDontBindLiteral() {
        EntityManager em = createEntityManager();

        Query controlQuery = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT(:param1, :param2)");
        controlQuery.setParameter("param1", "A").setParameter("param2", "B");
        List controlResults = controlQuery.getResultList();
        int nControlParams = ((ExpressionQueryMechanism)((EJBQueryImpl)controlQuery).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
        if(nControlParams != 2) {
            fail("controlQuery has wrong number of parameters = "+nControlParams+"; 2 is expected");
        }

        Query query = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT('A', 'B')");
        List results = query.getResultList();
        int nParams = ((ExpressionQueryMechanism)((EJBQueryImpl)query).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
        if(nParams > 0) {
            fail("Query processed literals as parameters");
        }

        closeEntityManager(em);
    }*/
public void testPersistenceProperties() {
    // Different properties are used on the server.
    if (isOnServer()) {
        return;
    }
    EntityManager em = createEntityManager();
    ServerSession ss = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em).getServerSession();
    // these properties were set in persistence unit
    // and overridden in CMP3TestModel.setup - the values should be overridden.
    boolean isReadShared = (ss.getReadConnectionPool() instanceof ReadConnectionPool);
    if (isReadShared != Boolean.parseBoolean((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_SHARED))) {
        fail("isReadShared is wrong");
    }
    int writeMin = ss.getDefaultConnectionPool().getMinNumberOfConnections();
    if (writeMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN))) {
        fail("writeMin is wrong");
    }
    int writeInitial = ss.getDefaultConnectionPool().getInitialNumberOfConnections();
    if (writeInitial != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_INITIAL))) {
        fail("writeInitial is wrong");
    }
    int writeMax = ss.getDefaultConnectionPool().getMaxNumberOfConnections();
    if (writeMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MAX))) {
        fail("writeMax is wrong");
    }
    int readMin = ss.getReadConnectionPool().getMinNumberOfConnections();
    if (readMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN))) {
        fail("readMin is wrong");
    }
    int readMax = ss.getReadConnectionPool().getMaxNumberOfConnections();
    if (readMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MAX))) {
        fail("readMax is wrong");
    }
    int batchSize = ss.getPlatform().getMaxBatchWritingSize();
    if (batchSize != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.BATCH_WRITING_SIZE))) {
        fail("batchSize is wrong");
    }
    // these properties were set in persistence unit - the values should be the same as in persistence.xml
    /*
            <property name="eclipselink.session-name" value="default-session"/>
            <property name="eclipselink.cache.size.default" value="500"/>
            <property name="eclipselink.cache.size.Employee" value="550"/>
            <property name="eclipselink.cache.size.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="555"/>
            <property name="eclipselink.cache.type.default" value="Full"/>
            <property name="eclipselink.cache.type.Employee" value="Weak"/>
            <property name="eclipselink.cache.type.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="HardWeak"/>
            <property name="eclipselink.session.customizer" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.Employee" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.Project" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
        */
    String sessionName = ss.getName();
    if (!sessionName.equals("default-session")) {
        fail("sessionName is wrong: " + sessionName);
    }
    int defaultCacheSize = ss.getDescriptor(Project.class).getIdentityMapSize();
    if (defaultCacheSize != 500) {
        fail("defaultCacheSize is wrong: " + defaultCacheSize);
    }
    int employeeCacheSize = ss.getDescriptor(Employee.class).getIdentityMapSize();
    if (employeeCacheSize != 550) {
        fail("employeeCacheSize is wrong: " + employeeCacheSize);
    }
    int addressCacheSize = ss.getDescriptor(Address.class).getIdentityMapSize();
    if (addressCacheSize != 555) {
        fail("addressCacheSize is wrong: " + addressCacheSize);
    }
    // Department cache size specified in @Cache annotation - that should override default property
    int departmentCacheSize = ss.getDescriptor(Department.class).getIdentityMapSize();
    if (departmentCacheSize != 777) {
        fail("departmentCacheSize is wrong: " + departmentCacheSize);
    }
    Class<?> defaultCacheType = ss.getDescriptor(Project.class).getIdentityMapClass();
    if (!Helper.getShortClassName(defaultCacheType).equals("FullIdentityMap")) {
        fail("defaultCacheType is wrong: " + Helper.getShortClassName(defaultCacheType));
    }
    Class<?> employeeCacheType = ss.getDescriptor(Employee.class).getIdentityMapClass();
    if (!Helper.getShortClassName(employeeCacheType).equals("WeakIdentityMap")) {
        fail("employeeCacheType is wrong: " + Helper.getShortClassName(employeeCacheType));
    }
    Class<?> addressCacheType = ss.getDescriptor(Address.class).getIdentityMapClass();
    if (!Helper.getShortClassName(addressCacheType).equals("HardCacheWeakIdentityMap")) {
        fail("addressCacheType is wrong: " + Helper.getShortClassName(addressCacheType));
    }
    // Department cache type specified in @Cache annotation - that should override default property
    Class<?> departmentCacheType = ss.getDescriptor(Department.class).getIdentityMapClass();
    if (!Helper.getShortClassName(departmentCacheType).equals("SoftCacheWeakIdentityMap")) {
        fail("departmentCacheType is wrong: " + Helper.getShortClassName(departmentCacheType));
    }
    int numSessionCalls = Customizer.getNumberOfCallsForSession(ss.getName());
    if (numSessionCalls == 0) {
        fail("session customizer hasn't been called");
    }
    int numProjectCalls = Customizer.getNumberOfCallsForClass(Project.class.getName());
    if (numProjectCalls == 0) {
        fail("Project customizer hasn't been called");
    }
    int numEmployeeCalls = Customizer.getNumberOfCallsForClass(Employee.class.getName());
    if (numEmployeeCalls == 0) {
        fail("Employee customizer hasn't been called");
    }
    int numAddressCalls = Customizer.getNumberOfCallsForClass(Address.class.getName());
    if (numAddressCalls == 0) {
        fail("Address customizer hasn't been called");
    }
    IdValidation employeeIdValidation = ss.getDescriptor(Employee.class).getIdValidation();
    if (employeeIdValidation != IdValidation.ZERO) {
        fail("employeeIdValidation is wrong, IdValidation.ZERO assigned through PrimaryKey annotation was expected");
    }
    IdValidation addressIdValidation = ss.getDescriptor(Address.class).getIdValidation();
    if (addressIdValidation != IdValidation.NEGATIVE) {
        fail("addressIdValidation is wrong, IdValidation.NEGATIVE set as a default value in persistence.xml was expected");
    }
    closeEntityManager(em);
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) IdValidation(org.eclipse.persistence.annotations.IdValidation) ReadConnectionPool(org.eclipse.persistence.sessions.server.ReadConnectionPool) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 22 with Address

use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testReadOnlyCachedLazyAssociation.

public void testReadOnlyCachedLazyAssociation() {
    EntityManager em = createEntityManager();
    Integer empId = null;
    Employee emp = null;
    try {
        // setup
        try {
            beginTransaction(em);
            emp = new Employee();
            emp.setFirstName("Mark");
            emp.setLastName("Dowder");
            final PhoneNumber phone = new PhoneNumber("work", "613", "5555555");
            emp.addPhoneNumber(phone);
            final PhoneNumber newPhone = new PhoneNumber("home", "613", "4444444");
            emp.addPhoneNumber(newPhone);
            final Address address = new Address("SomeStreet", "somecity", "province", "country", "postalcode");
            emp.setAddress(address);
            em.persist(emp);
            em.flush();
            commitTransaction(em);
            empId = emp.getId();
            // clear cache
            em.getEntityManagerFactory().getCache().evictAll();
            getServerSession().getIdentityMapAccessor().initializeIdentityMaps();
        } catch (RuntimeException ex) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
            throw ex;
        }
        final Employee employee = em.find(Employee.class, empId);
        Assert.assertNotNull("No Employee retrieved", employee);
        // Trigger Bug#474232
        employee.getAddress();
        final Employee cachedEmployee = em.createNamedQuery("findEmployeeByPK", Employee.class).setParameter("id", empId).setHint(QueryHints.READ_ONLY, HintValues.TRUE).getSingleResult();
        Assert.assertNotNull("Employee not found", cachedEmployee);
        final Address address = cachedEmployee.getAddress();
        Assert.assertNotNull("Address of employee not retrieved", address);
    } finally {
        // Clean up
        if (empId != null) {
            beginTransaction(em);
            em.remove(em.merge(emp));
            commitTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 23 with Address

use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testNoPersistOnFlushProperties.

// Test Not using the persist operation on commit.
public void testNoPersistOnFlushProperties() {
    // 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);
        em.flush();
        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);
        em.flush();
        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
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with Address

use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testPersistOnCommit.

// Test using PERSISTENCE_CONTEXT_PERSIST_ON_COMMIT option to avoid the discover on commit.
public void testPersistOnCommit() {
    // Properties only works in jse.
    if (isOnServer()) {
        return;
    }
    Map properties = new HashMap();
    properties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_PERSIST_ON_COMMIT, "false");
    EntityManager em = createEntityManager(properties);
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Douglas");
        emp.setLastName("McRae");
        em.persist(emp);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(emp);
        closeEntityManager(em);
        em = createEntityManager(properties);
        beginTransaction(em);
        Address address = new Address();
        emp = em.find(Employee.class, emp.getId());
        emp.setAddress(address);
        em.persist(address);
        commitTransaction(em);
        verifyObjectInCacheAndDatabase(emp);
        em = createEntityManager(properties);
        beginTransaction(em);
        emp = em.find(Employee.class, emp.getId());
        em.remove(emp);
        commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException exception) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw exception;
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with Address

use of org.eclipse.persistence.testing.models.jpa.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);
    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.");
    }
    commitTransaction(em);
    closeEntityManager(em);
    em = createEntityManager();
    beginTransaction(em);
    employee = em.find(Employee.class, clone.getId());
    clone = employee.clone();
    clone.setId(null);
    em.persist(clone);
    commitTransaction(em);
    if (clone.getId() == null) {
        fail("Clone was not persisted.");
    }
    beginTransaction(em);
    employee = em.find(Employee.class, clone.getId());
    em.remove(employee);
    commitTransaction(em);
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address)

Aggregations

Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)79 EntityManager (jakarta.persistence.EntityManager)76 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)53 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)46 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)13 Query (jakarta.persistence.Query)12 QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)12 ArrayList (java.util.ArrayList)9 List (java.util.List)9 HashMap (java.util.HashMap)7 TypedQuery (jakarta.persistence.TypedQuery)6 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)6 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)6 Test (org.junit.Test)6 EntityExistsException (jakarta.persistence.EntityExistsException)5 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)5 Map (java.util.Map)5 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)5 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)5 PersistenceException (jakarta.persistence.PersistenceException)4