Skip to content

Commit 2b82b97

Browse files
authored
Merge pull request #2 from jGauravGupta/JAVAEE8-JPA-STREAM
JPA Result Stream Test
2 parents 282debd + c63c1ec commit 2b82b97

File tree

9 files changed

+185
-0
lines changed

9 files changed

+185
-0
lines changed

jpa/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?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"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>samples-parent</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>jpa</artifactId>
11+
<packaging>pom</packaging>
12+
<name>Java EE 8 Samples: JPA</name>
13+
14+
<modules>
15+
<module>stream</module>
16+
</modules>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee8</groupId>
21+
<artifactId>test-utils</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
</dependencies>
25+
</project>

jpa/stream/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?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"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>jpa</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>stream</artifactId>
11+
<packaging>war</packaging>
12+
<name>Java EE 8 Samples: JPA - Stream</name>
13+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.javaee8.jpa.stream.domain;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Basic;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
10+
/**
11+
*
12+
* @author Gaurav Gupta
13+
*
14+
*/
15+
@Entity
16+
public class Person implements Serializable {
17+
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.AUTO)
20+
private Long id;
21+
22+
@Basic
23+
private String name;
24+
25+
@Basic
26+
private String address;
27+
28+
public Long getId() {
29+
return this.id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getName() {
37+
return this.name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public String getAddress() {
45+
return this.address;
46+
}
47+
48+
public void setAddress(String address) {
49+
this.address = address;
50+
}
51+
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javaee8.jpa.stream.repository;
2+
3+
import java.util.stream.Stream;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
import javax.persistence.criteria.CriteriaQuery;
7+
import org.javaee8.jpa.stream.domain.Person;
8+
9+
/**
10+
*
11+
* @author Gaurav Gupta
12+
*
13+
*/
14+
public class PersonRepository {
15+
16+
@PersistenceContext(unitName = "DEFAULT_PU")
17+
private EntityManager em;
18+
19+
public Stream<Person> findAll() {
20+
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
21+
cq.select(cq.from(Person.class));
22+
return em.createQuery(cq).getResultStream();
23+
}
24+
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javaee8.jpa.stream.controller;
2+
3+
import java.util.stream.Stream;
4+
import org.javaee8.jpa.stream.repository.PersonRepository;
5+
import org.javaee8.jpa.stream.domain.Person;
6+
import javax.inject.Inject;
7+
import org.jboss.arquillian.container.test.api.Deployment;
8+
import org.jboss.arquillian.junit.Arquillian;
9+
import org.jboss.shrinkwrap.api.ShrinkWrap;
10+
import org.junit.Test;
11+
import org.jboss.shrinkwrap.api.spec.WebArchive;
12+
import static org.junit.Assert.assertEquals;
13+
import org.junit.runner.RunWith;
14+
15+
/**
16+
*
17+
* @author Gaurav Gupta
18+
*
19+
*/
20+
@RunWith(Arquillian.class)
21+
public class PersonControllerTest {
22+
23+
@Deployment
24+
public static WebArchive createDeployment() {
25+
return ShrinkWrap.create(WebArchive.class)
26+
.addAsWebInfResource("beans.xml")
27+
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
28+
.addAsResource("META-INF/sql/insert.sql")
29+
.addClass(Person.class)
30+
.addClass(PersonRepository.class);
31+
}
32+
33+
@Inject
34+
private PersonRepository personRepository;
35+
36+
@Test
37+
public void testStream() throws Exception {
38+
Stream<Person> personStream = personRepository.findAll();
39+
long personCount = personStream.count();
40+
assertEquals(2, personCount);
41+
}
42+
43+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO person (id, name, address) VALUES (1002, 'Arjan', 'abc')
2+
INSERT INTO person (id, name, address) VALUES (1001, 'Gaurav', 'xyz')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
5+
bean-discovery-mode="all">
6+
</beans>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" 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_1.xsd">
3+
<persistence-unit name="DEFAULT_PU" transaction-type="JTA">
4+
<jta-data-source>java:comp/DefaultDataSource</jta-data-source>
5+
<exclude-unlisted-classes>false</exclude-unlisted-classes>
6+
<properties>
7+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
8+
<property name="javax.persistence.sql-load-script-source" value="META-INF/sql/insert.sql"/>
9+
</properties>
10+
</persistence-unit>
11+
</persistence>

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<module>servlet</module>
7373
<module>jsf</module>
7474
<module>cdi</module>
75+
<module>jpa</module>
7576
<module>jsonb</module>
7677
</modules>
7778

@@ -124,6 +125,13 @@
124125
<scope>provided</scope>
125126
</dependency>
126127

128+
<dependency>
129+
<groupId>org.eclipse.persistence</groupId>
130+
<artifactId>javax.persistence</artifactId>
131+
<version>2.2.0-RC1</version>
132+
<scope>provided</scope>
133+
</dependency>
134+
127135
<dependency>
128136
<groupId>javax.ws.rs</groupId>
129137
<artifactId>javax.ws.rs-api</artifactId>

0 commit comments

Comments
 (0)