Search in sources :

Example 1 with GenericEntity

use of org.eclipse.persistence.jpa.test.property.model.GenericEntity in project eclipselink by eclipse-ee4j.

the class TestParameterBinding method testNUMERICALEXPRESSION_ForceBindJPQLParameters.

@Test
public void testNUMERICALEXPRESSION_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test numerical expression with untyped parameters + typed parameters
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (s.itemInteger1 + ?4) FROM GenericEntity s " + "WHERE (s.itemInteger1 + ?4) > 1", GenericEntity.class);
        query.setParameter(4, 2);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 3, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test numerical expression with parameters
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (?3 + ?4) FROM GenericEntity s " + "WHERE (?3 + ?4) > 1", GenericEntity.class);
        query.setParameter(3, 2);
        query.setParameter(4, 2);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDerby()) {
            Assert.fail("Expected a failure from " + pl);
        }
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        }
    } catch (PersistenceException e) {
        Platform pl = forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        // When all the operands of a numeric expression are untyped parameters, error 42X35 on Derby
        if (pl.isDerby()) {
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("java.sql.SQLSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else {
            Assert.fail("Unexpected failure: " + e);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test numerical expression with literals
        TypedQuery<GenericEntity> query = em.createQuery("SELECT (s.itemInteger1 + 4) FROM GenericEntity s " + "WHERE ABS(s.itemInteger1 + 4) > 1", GenericEntity.class);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 3, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 0, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Example 2 with GenericEntity

use of org.eclipse.persistence.jpa.test.property.model.GenericEntity in project eclipselink by eclipse-ee4j.

the class TestParameterBinding method testCONCAT_ForceBindJPQLParameters.

@Test
public void testCONCAT_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // 1: Test string CONCAT with untyped parameter and literal
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 = TRIM(CONCAT(?1 , '-'))" + "AND s.itemString1 = TRIM(CONCAT(?2 , '-'))", GenericEntity.class);
        query.setParameter(1, "1");
        query.setParameter(2, "99");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // 1: Test string CONCAT with untyped parameter and untyped parameter
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 = TRIM(CONCAT(?1 , ?1))" + "AND s.itemString1 = TRIM(CONCAT(?2 , ?2))", GenericEntity.class);
        query.setParameter(1, "1");
        query.setParameter(2, "99");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDB2Z() || pl.isDerby()) {
            Assert.fail("Expected a failure from " + pl);
        }
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        }
    } catch (PersistenceException e) {
        Platform pl = forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        // When both operands of a CONCAT operator are untyped parameters, error on DB2/z
        if (pl.isDB2Z()) {
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("com.ibm.db2.jcc.am.SqlSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else if (pl.isDerby()) {
            // When all the operands of '||' expression are untyped parameters, error 42X35 on Derby
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("java.sql.SQLSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else {
            Assert.fail("Unexpected failure: " + e);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Example 3 with GenericEntity

use of org.eclipse.persistence.jpa.test.property.model.GenericEntity in project eclipselink by eclipse-ee4j.

the class TestParameterBinding method testSUBSTR_ForceBindJPQLParameters.

@Test
public void testSUBSTR_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test untyped parameter is first argument of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE TRIM(s.itemString1) = TRIM(SUBSTRING(?1, 1, 5))", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 1, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test untyped parameter is first & second argument of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE TRIM(s.itemString1) = TRIM(SUBSTRING(?1, ?2, 5))", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        query.setParameter(2, 1);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test untyped parameter is all arguments of SUBSTRING
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE s.itemString1 = SUBSTRING(?1, ?2, ?3)", GenericEntity.class);
        query.setParameter(1, "HELLO WORLD");
        query.setParameter(2, 1);
        query.setParameter(3, 5);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 3, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test SUBSTRING function with IN expression
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE SUBSTRING(s.itemString1, 1, ?1) NOT IN (?2, ?3, ?4, ?5)", GenericEntity.class);
        query.setParameter(1, 5);
        query.setParameter(2, "TEST1");
        query.setParameter(3, "TEST2");
        query.setParameter(4, "HELLO");
        query.setParameter(5, "TEST3");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 7, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Test(org.junit.Test)

Example 4 with GenericEntity

use of org.eclipse.persistence.jpa.test.property.model.GenericEntity in project eclipselink by eclipse-ee4j.

the class TestParameterBinding method testIN_ForceBindJPQLParameters.

@Test
public void testIN_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test all the operands of an IN predicate
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (?2, ?3, ?4)", GenericEntity.class);
        query.setParameter(1, 4);
        query.setParameter(2, 4);
        query.setParameter(3, 5);
        query.setParameter(4, 6);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDB2Z() || pl.isDerby()) {
            Assert.fail("Expected a failure from " + pl);
        }
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        }
    } catch (PersistenceException e) {
        Platform pl = forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.isDB2Z()) {
            // When all the operands of an IN predicate are untyped parameters, error on DB2/z
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("com.ibm.db2.jcc.am.SqlSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else if (pl.isDerby()) {
            // Use as the left operand of an IN list is not allowed when all operands are untyped parameters, error 42X35 on Derby
            Assert.assertEquals(DatabaseException.class, e.getCause().getClass());
            Assert.assertEquals("java.sql.SQLSyntaxErrorException", e.getCause().getCause().getClass().getName());
        } else {
            Assert.fail("Unexpected failure: " + e);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first and second operands of an IN predicate
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (?2, 'b', 'c')", GenericEntity.class);
        query.setParameter(1, "a");
        query.setParameter(2, "a");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of an IN predicate and zero or more operands of the IN list except for the first operand of the IN list
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE ?1 IN (5, ?2, 6)", GenericEntity.class);
        query.setParameter(1, 4);
        query.setParameter(2, 4);
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 5, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test any or all operands of the IN list of the IN predicate and the first operand of the IN predicate is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 2 FROM GenericEntity s " + "WHERE s.itemString1 IN (?1, 'b', ?2)", GenericEntity.class);
        query.setParameter(1, "a");
        query.setParameter(2, "c");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Example 5 with GenericEntity

use of org.eclipse.persistence.jpa.test.property.model.GenericEntity in project eclipselink by eclipse-ee4j.

the class TestParameterBinding method testLIKE_ForceBindJPQLParameters.

@Test
public void testLIKE_ForceBindJPQLParameters() {
    EntityManager em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of a LIKE predicate (the match-expression) is untyped parameter
        // when at least one other operand (the pattern-expression or escape-expression)
        // is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE ?1 LIKE ?2 ESCAPE '_'", GenericEntity.class);
        query.setParameter(1, "HELLO_WORLD");
        query.setParameter(2, "HELLO%");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 4, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
    em = forceBindEMF.createEntityManager();
    try {
        // Test the first operand of a LIKE predicate (the match-expression) is untyped parameter
        // when at least one other operand (the pattern-expression or escape-expression)
        // is not an untyped parameter marker
        TypedQuery<GenericEntity> query = em.createQuery("SELECT 1 FROM GenericEntity s " + "WHERE s.itemString1 LIKE ?2", GenericEntity.class);
        query.setParameter(2, "HELLO%");
        query.getResultList();
        DatabaseCall call = ((JpaQuery<GenericEntity>) query).getDatabaseQuery().getCall();
        Assert.assertFalse("Expected query parameter binding to not be set for the DatabaseCall", call.isUsesBindingSet());
        DatabasePlatform pl = (DatabasePlatform) forceBindEMF.unwrap(EntityManagerFactoryImpl.class).getDatabaseSession().getDatasourcePlatform();
        if (pl.shouldBindLiterals()) {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 2, call.getParameters().size());
        } else {
            Assert.assertEquals("The number of parameters found does not match the number supplied", 1, call.getParameters().size());
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) EntityManager(jakarta.persistence.EntityManager) GenericEntity(org.eclipse.persistence.jpa.test.property.model.GenericEntity) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) Test(org.junit.Test)

Aggregations

EntityManager (jakarta.persistence.EntityManager)8 DatabaseCall (org.eclipse.persistence.internal.databaseaccess.DatabaseCall)8 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)8 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)8 GenericEntity (org.eclipse.persistence.jpa.test.property.model.GenericEntity)8 Test (org.junit.Test)8 PersistenceException (jakarta.persistence.PersistenceException)5 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)5 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)5