Search in sources :

Example 76 with Address

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

the class JUnitNativeQueryTestSuite method testNativeQueryWithMixedCaseFields.

/* Removed for 299926 when the default changed to false.
     * force results to be returned in camel case - any case that is different from what is defined in the Entity's field definitions
     */
public void testNativeQueryWithMixedCaseFields() {
    EntityManager em = createEntityManager(PUName);
    try {
        Address expectedAddress = (Address) em.createQuery("select a from Address a").getResultList().get(0);
        Address returnedAddress = null;
        ServerSession session = JUnitTestCase.getServerSession();
        String delimiter = session.getPlatform().getStartDelimiter();
        Query q = em.createNativeQuery("Select ADDRESS_ID as " + delimiter + "aDdReSs_iD" + delimiter + ", P_CODE as " + delimiter + "P_cOdE" + delimiter + ", STREET as " + delimiter + "StReeT" + delimiter + ", PROVINCE as " + delimiter + "PrOvInCe" + delimiter + ", TYPE as " + delimiter + "TyPe" + delimiter + ", CITY as " + delimiter + "CiTy" + delimiter + ", COUNTRY as " + delimiter + "CoUnTrY" + delimiter + ", VERSION as " + delimiter + "VeRsIoN" + delimiter + " from CMP3_ADDRESS where ADDRESS_ID = " + expectedAddress.getID(), Address.class);
        returnedAddress = (Address) q.getSingleResult();
        assertNotNull("no address returned", returnedAddress);
        assertTrue("returned address does not match the expected address", session.compareObjects(returnedAddress, expectedAddress));
        // this query uses a resultsetmapping that looks for columns as camel case.
        q = em.createNativeQuery("Select ADDRESS_ID" + /* as " + delimiter + "aDrEsS_iD" +delimiter*/
        ", P_CODE" + /* as "+ delimiter + "P_cOdE" +delimiter*/
        ", STREET" + /* as "+ delimiter + "StReeT" +delimiter*/
        ", PROVINCE" + /* as "+ delimiter + "PrOvInCe" +delimiter*/
        ", TYPE" + /* as " + delimiter + "TyPe" +delimiter*/
        ", CITY" + /* as " + delimiter + "CiTy" +delimiter*/
        ", COUNTRY" + /* as " + delimiter + "CoUnTy" +delimiter*/
        ", VERSION" + /* as " + delimiter + "VeRsIoN" +delimiter*/
        " from CMP3_ADDRESS where ADDRESS_ID = " + expectedAddress.getID(), "address-case-sensitive-map");
        returnedAddress = (Address) q.getSingleResult();
        assertNotNull("no address returned", returnedAddress);
        assertTrue("returned address does not match the expected address", session.compareObjects(returnedAddress, expectedAddress));
    } finally {
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Query(jakarta.persistence.Query)

Example 77 with Address

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

the class JUnitNativeQueryTestSuite method testNativeQueryCacheHit.

/**
 * 334704 - native queries do not use the shared cache
 */
public void testNativeQueryCacheHit() {
    Address result = null;
    EntityManager em = createEntityManager(PUName);
    Query query = em.createQuery("Select a from Address a");
    Address a = (Address) query.getResultList().get(0);
    closeEntityManager(em);
    // test needs to use a new EM so that the object returned is from the shared cache.
    em = createEntityManager(PUName);
    try {
        this.beginTransaction(em);
        query = em.createNativeQuery("Select ADDRESS_ID from CMP3_ADDRESS where ADDRESS_ID=" + a.getID(), Address.class);
        result = (Address) query.getSingleResult();
    } finally {
        rollbackTransaction(em);
        clearCache(PUName);
        closeEntityManager(em);
    }
    assertTrue("Street from Address read using native query did not match the cache version", result.getStreet() != null && result.getStreet().equals(a.getStreet()));
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Query(jakarta.persistence.Query)

Example 78 with Address

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

the class JUnitJPQLSimpleTestSuite method simpleEscapeUnderscoreTest.

public void simpleEscapeUnderscoreTest() {
    EntityManager em = createEntityManager();
    Address expectedResult = new Address();
    expectedResult.setCity("Perth");
    expectedResult.setCountry("Canada");
    expectedResult.setProvince("ONT");
    expectedResult.setPostalCode("Y3Q2N9");
    expectedResult.setStreet("234 Wandering _Way");
    Server serverSession = JUnitTestCase.getServerSession();
    Session clientSession = serverSession.acquireClientSession();
    UnitOfWork uow = clientSession.acquireUnitOfWork();
    uow.registerObject(expectedResult);
    uow.commit();
    // test the apostrophe
    String ejbqlString = "SELECT OBJECT(address) FROM Address address WHERE ";
    // \ is always treated as escape in MySQL.  Therefore ESCAPE '\' is considered a syntax error
    if (getServerSession().getPlatform().isMySQL() || getServerSession().getPlatform().isPostgreSQL()) {
        ejbqlString = ejbqlString + "address.street LIKE '234 Wandering $_Way' ESCAPE '$'";
    } else {
        ejbqlString = ejbqlString + "address.street LIKE '234 Wandering \\_Way' ESCAPE '\\'";
    }
    List result = em.createQuery(ejbqlString).getResultList();
    Assert.assertTrue("Simple Escape Underscore test failed", comparer.compareObjects(result, expectedResult));
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Server(org.eclipse.persistence.sessions.server.Server) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 79 with Address

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

the class JUnitJPQLSimpleTestSuite method simpleLikeEscapeTestWithParameter.

public void simpleLikeEscapeTestWithParameter() {
    EntityManager em = createEntityManager();
    Address expectedResult = new Address();
    expectedResult.setCity("TAIYUAN");
    expectedResult.setCountry("CHINA");
    expectedResult.setProvince("SHANXI");
    expectedResult.setPostalCode("030024");
    expectedResult.setStreet("234 RUBY _Way");
    Server serverSession = JUnitTestCase.getServerSession();
    Session clientSession = serverSession.acquireClientSession();
    UnitOfWork uow = clientSession.acquireUnitOfWork();
    uow.registerObject(expectedResult);
    uow.commit();
    // test the apostrophe
    String ejbqlString = "SELECT OBJECT(address) FROM Address address WHERE address.street LIKE :pattern ESCAPE :esc";
    String patternString = null;
    Character escChar = null;
    // \ is always treated as escape in MySQL.  Therefore ESCAPE '\' is considered a syntax error
    if (getServerSession().getPlatform().isMySQL()) {
        patternString = "234 RUBY $_Way";
        escChar = '$';
    } else {
        patternString = "234 RUBY \\_Way";
        escChar = '\\';
    }
    List result = em.createQuery(ejbqlString).setParameter("pattern", patternString).setParameter("esc", escChar).getResultList();
    Assert.assertTrue("Simple Escape Underscore test failed", comparer.compareObjects(result, expectedResult));
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) Server(org.eclipse.persistence.sessions.server.Server) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

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