Skip to content

Commit 22722f7

Browse files
committed
Merge pull request #198 from radcortez/master
Tests for native-sql and native-sql-resultset-mapping projects
2 parents 1ae3dc0 + 5442de1 commit 22722f7

File tree

12 files changed

+182
-429
lines changed

12 files changed

+182
-429
lines changed

jpa/listeners/src/test/java/org/javaee7/jpa/listeners/JpaListenersTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
/**
1818
* In this sample we're going to query a simple +JPA Entity+, using the +JPA EntityManager+ and perform a findAll,
1919
* persist, merge, and remove operations. By calling these operations we want to demonstrate the behaviour of +Entity
20-
* Listener Methods+ defined on +MovieListener+:
21-
* +@PostLoad+
22-
* +@PrePersist+
23-
* +@PostPersist+
24-
* +@PreUpdate+
25-
* +@PostUpdate+
26-
* +@PreRemove+
27-
* +@PostRemove+
20+
* Listener Methods+: +@PostLoad+, +@PrePersist+, +@PostPersist+, +@PreUpdate+, +@PostUpdate+, +@PreRemove+,
21+
* +@PostRemove+ defined on +MovieListener+:
22+
*
23+
* include::MovieListener[]
2824
*
2925
* The following +JPA Entity+, represents a Movie which has a name and a comma separated list of actors:
3026
*
3127
* include::Movie[]
3228
*
29+
*
3330
* @author Roberto Cortez
3431
*/
3532
@RunWith(Arquillian.class)
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>org.javaee7.jpa</groupId>
67
<artifactId>jpa-samples</artifactId>
78
<version>1.0-SNAPSHOT</version>
8-
<relativePath>../pom.xml</relativePath>
9+
<relativePath>../pom.xml</relativePath>
910
</parent>
1011

11-
<groupId>org.javaee7.jpa</groupId>
1212
<artifactId>native-sql-resultset-mapping</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1413
<packaging>war</packaging>
14+
<name>JPA Native SQL ResultSet Mapping</name>
15+
<description>Using the EntityManager API to perform native SQL queries and map the result with @SqlResultSetMapping
16+
annotation
17+
</description>
1518
</project>

jpa/native-sql-resultset-mapping/src/main/java/org/javaee7/jpa/nativesql/resultset/mapping/Employee.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,38 @@
4040
package org.javaee7.jpa.nativesql.resultset.mapping;
4141

4242
import java.io.Serializable;
43-
import javax.persistence.Column;
44-
import javax.persistence.Entity;
45-
import javax.persistence.EntityResult;
46-
import javax.persistence.Id;
47-
import javax.persistence.SqlResultSetMapping;
48-
import javax.persistence.Table;
43+
import javax.persistence.*;
4944

5045
/**
5146
* @author Arun Gupta
5247
*/
5348
@Entity
54-
@Table(name="EMPLOYEE_NATIVE_SQL_RESULTSET_MAPPING")
49+
@Table(name = "EMPLOYEE_NATIVE_SQL_RESULTSET_MAPPING")
5550
@SqlResultSetMapping(name = "myMapping",
56-
entities = {@EntityResult(entityClass = Employee.class)})
51+
entities = {@EntityResult(entityClass = Employee.class,
52+
fields = {@FieldResult(name = "identifier", column = "id"),
53+
@FieldResult(name = "simpleName", column = "name")})})
5754
public class Employee implements Serializable {
5855
private static final long serialVersionUID = 1L;
5956
@Id
60-
private int id;
61-
62-
@Column(length=50)
63-
private String name;
64-
65-
public Employee() { }
66-
67-
public Employee(String name) {
68-
this.name = name;
69-
}
70-
71-
public int getId() {
72-
return id;
57+
private int identifier;
58+
59+
@Column(length = 50)
60+
private String simpleName;
61+
62+
public int getIdentifier() {
63+
return identifier;
7364
}
7465

75-
public void setId(int id) {
76-
this.id = id;
66+
public void setIdentifier(int identifier) {
67+
this.identifier = identifier;
7768
}
7869

79-
public String getName() {
80-
return name;
70+
public String getSimpleName() {
71+
return simpleName;
8172
}
8273

83-
public void setName(String name) {
84-
this.name = name;
74+
public void setSimpleName(String simpleName) {
75+
this.simpleName = simpleName;
8576
}
8677
}

jpa/native-sql-resultset-mapping/src/main/java/org/javaee7/jpa/nativesql/resultset/mapping/EmployeeBean.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@
4949
*/
5050
@Stateless
5151
public class EmployeeBean {
52-
5352
@PersistenceContext
54-
EntityManager em;
55-
56-
public void persist(Employee e) {
57-
em.persist(e);
58-
}
59-
53+
private EntityManager em;
54+
55+
@SuppressWarnings("unchecked")
6056
public List<Employee> get() {
6157
return em.createNativeQuery("select * from EMPLOYEE_NATIVE_SQL_RESULTSET_MAPPING", "myMapping").getResultList();
6258
}

jpa/native-sql-resultset-mapping/src/main/java/org/javaee7/jpa/nativesql/resultset/mapping/TestServlet.java

Lines changed: 0 additions & 127 deletions
This file was deleted.

jpa/native-sql-resultset-mapping/src/main/webapp/index.jsp

Lines changed: 0 additions & 59 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.javaee7.jpa.nativesql.resultset.mapping;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.shrinkwrap.api.ShrinkWrap;
6+
import org.jboss.shrinkwrap.api.spec.WebArchive;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import javax.inject.Inject;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertFalse;
15+
16+
/**
17+
* In this sample we're going to query a simple +JPA Entity+, using the +JPA EntityManager Native Query+, perform
18+
* a select operation and map the query result using +@SqlResultSetMapping+.
19+
*
20+
* include::Employee[]
21+
*
22+
* The select operation is very simple. We just need to call the API method +createNativeQuery+ on the +EntityManager+
23+
* and use the mapping defined on +Employee+ by the +@SqlResultSetMapping+ annotation.
24+
*
25+
* include::EmployeeBean[]#get
26+
*
27+
* @author Roberto Cortez
28+
*/
29+
@RunWith(Arquillian.class)
30+
public class JpaNativeSqlResultSetMappingTest {
31+
@Inject
32+
private EmployeeBean employeeBean;
33+
34+
/**
35+
* We're just going to deploy the application as a +web archive+. Note the inclusion of the following files:
36+
*
37+
* [source,file]
38+
* ----
39+
* /META-INF/persistence.xml
40+
* /META-INF/create.sql
41+
* /META-INF/drop.sql
42+
* /META-INF/load.sql
43+
* ----
44+
*
45+
* The +persistence.xml+ file is needed of course for the persistence unit definition. A datasource is not
46+
* needed, since we can now use the new default datasource available in +JEE7+. We're also using the new
47+
* +javax.persistence.schema-generation.*+ propertires to create, populate and drop the database.
48+
*/
49+
@Deployment
50+
public static WebArchive createDeployment() {
51+
WebArchive war = ShrinkWrap.create(WebArchive.class)
52+
.addPackage("org.javaee7.jpa.nativesql.resultset.mapping")
53+
.addAsResource("META-INF/persistence.xml")
54+
.addAsResource("META-INF/create.sql")
55+
.addAsResource("META-INF/drop.sql")
56+
.addAsResource("META-INF/load.sql");
57+
System.out.println(war.toString(true));
58+
return war;
59+
}
60+
61+
/**
62+
* In the test, we're just going to invoke the only available operation in the +EmployeeBean+ and assert a few
63+
* details to confirm that the native query was successfully executed.
64+
*/
65+
@Test
66+
public void testJpaNativeSqlResultSetMapping() {
67+
List<Employee> employees = employeeBean.get();
68+
assertFalse(employees.isEmpty());
69+
assertEquals(8, employees.size());
70+
}
71+
}

0 commit comments

Comments
 (0)