Merge pull request #7309 from tryncatch/BAEL-2804
BAEL-2804 JPA Query Parameters Usage
This commit is contained in:
commit
43cfd56b9c
|
@ -1 +1,6 @@
|
|||
# Relevant Articles
|
||||
<<<<<<< HEAD
|
||||
|
||||
- [JPA Query Parameters Usage](http://www.baeldung.com/jpa-query-parameters-usage)
|
||||
=======
|
||||
>>>>>>> refs/heads/master
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-jpa-2</artifactId>
|
||||
<name>java-jpa-2</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
|
@ -30,14 +27,14 @@
|
|||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Compile time JPA API-->
|
||||
<!--Compile time JPA API -->
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>${javax.persistence-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Runtime JPA implementation-->
|
||||
<!--Runtime JPA implementation -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>eclipselink</artifactId>
|
||||
|
@ -104,7 +101,6 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.4.0.Final</hibernate.version>
|
||||
<eclipselink.version>2.7.4-RC1</eclipselink.version>
|
||||
|
@ -112,4 +108,4 @@
|
|||
<javax.persistence-api.version>2.2</javax.persistence-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
|
||||
version="2.2">
|
||||
|
||||
<persistence-unit name="jpa-h2-queryparams"
|
||||
transaction-type="RESOURCE_LOCAL">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.queryparams.Employee</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver"
|
||||
value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url"
|
||||
value="jdbc:h2:mem:test" />
|
||||
<property name="javax.persistence.jdbc.user"
|
||||
value="sa" />
|
||||
<property name="javax.persistence.jdbc.password"
|
||||
value="" />
|
||||
<property name="hibernate.dialect"
|
||||
value="org.hibernate.dialect.H2Dialect" />
|
||||
<property name="hibernate.hbm2ddl.auto"
|
||||
value="create-drop" />
|
||||
<property name="show_sql" value="true" />
|
||||
<property
|
||||
name="hibernate.temp.use_jdbc_metadata_defaults"
|
||||
value="false" />
|
||||
<property
|
||||
name="javax.persistence.sql-load-script-source"
|
||||
value="queryparams.sql" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT"
|
||||
class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
|
||||
%msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -36,7 +36,7 @@ public class JPAQueryParamsUnitTest {
|
|||
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = ?1", Employee.class);
|
||||
String empNumber = "A123";
|
||||
Employee employee = query.setParameter(1, empNumber)
|
||||
.getSingleResult();
|
||||
.getSingleResult();
|
||||
Assert.assertNotNull("Employee not found", employee);
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ public class JPAQueryParamsUnitTest {
|
|||
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber IN (?1)", Employee.class);
|
||||
List<String> empNumbers = Arrays.asList("A123", "A124");
|
||||
List<Employee> employees = query.setParameter(1, empNumbers)
|
||||
.getResultList();
|
||||
.getResultList();
|
||||
Assert.assertNotNull("Employees not found", employees);
|
||||
Assert.assertFalse("Employees not found", employees.isEmpty());
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public class JPAQueryParamsUnitTest {
|
|||
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = :number", Employee.class);
|
||||
String empNumber = "A123";
|
||||
Employee employee = query.setParameter("number", empNumber)
|
||||
.getSingleResult();
|
||||
.getSingleResult();
|
||||
Assert.assertNotNull("Employee not found", employee);
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class JPAQueryParamsUnitTest {
|
|||
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber IN (:numbers)", Employee.class);
|
||||
List<String> empNumbers = Arrays.asList("A123", "A124");
|
||||
List<Employee> employees = query.setParameter("numbers", empNumbers)
|
||||
.getResultList();
|
||||
.getResultList();
|
||||
Assert.assertNotNull("Employees not found", employees);
|
||||
Assert.assertFalse("Employees not found", employees.isEmpty());
|
||||
}
|
||||
|
@ -75,8 +75,8 @@ public class JPAQueryParamsUnitTest {
|
|||
String empName = "John Doe";
|
||||
int empAge = 55;
|
||||
List<Employee> employees = query.setParameter("name", empName)
|
||||
.setParameter("empAge", empAge)
|
||||
.getResultList();
|
||||
.setParameter("empAge", empAge)
|
||||
.getResultList();
|
||||
Assert.assertNotNull("Employees not found!", employees);
|
||||
Assert.assertTrue("Employees not found!", !employees.isEmpty());
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ public class JPAQueryParamsUnitTest {
|
|||
Root<Employee> c = cQuery.from(Employee.class);
|
||||
ParameterExpression<String> paramEmpNumber = cb.parameter(String.class);
|
||||
cQuery.select(c)
|
||||
.where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber));
|
||||
.where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber));
|
||||
|
||||
TypedQuery<Employee> query = entityManager.createQuery(cQuery);
|
||||
String empNumber = "A123";
|
|
@ -224,26 +224,4 @@
|
|||
value="products_jpa.sql" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="jpa-h2-queryparams" transaction-type="RESOURCE_LOCAL">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.queryparams.Employee</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver"
|
||||
value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url"
|
||||
value="jdbc:h2:mem:test" />
|
||||
<property name="javax.persistence.jdbc.user" value="sa" />
|
||||
<property name="javax.persistence.jdbc.password" value="" />
|
||||
<property name="hibernate.dialect"
|
||||
value="org.hibernate.dialect.H2Dialect" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
<property name="show_sql" value="true" />
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults"
|
||||
value="false" />
|
||||
<property name="javax.persistence.sql-load-script-source"
|
||||
value="employees2.sql" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -18,7 +18,6 @@
|
|||
<module>apache-cayenne</module>
|
||||
<module>core-java-persistence</module>
|
||||
<module>deltaspike</module>
|
||||
<module>elasticsearch</module>
|
||||
<module>flyway</module>
|
||||
<module>hbase</module>
|
||||
<module>hibernate5</module>
|
||||
|
|
Loading…
Reference in New Issue