Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions jpa/datasourcedefinition-annotation-pu/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.javaee7.jpa</groupId>
<artifactId>jpa-samples</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>datasourcedefinition-annotation-pu</artifactId>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.173</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.javaee7.jpa.datasourcedefinition_annotation_pu.config;

import javax.annotation.sql.DataSourceDefinition;
import javax.ejb.Stateless;

@DataSourceDefinition(
name = "java:app/MyApp/MyDS",
className = "org.h2.jdbcx.JdbcDataSource",
url = "jdbc:h2:mem:test"
)
@Stateless
public class DataSourceDefinitionConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.javaee7.jpa.datasourcedefinition_annotation_pu.entity;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* A very simple JPA entity that will be used for testing
*
* @author Arjan Tijms
*
*/
@Entity
public class TestEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String value;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.javaee7.jpa.datasourcedefinition_annotation_pu.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.javaee7.jpa.datasourcedefinition_annotation_pu.entity.TestEntity;

/**
* This service does some JPA operations. The purpose of this entire test
* is just to see whether the data source can be used so the actual operations
* don't matter much.
*
* @author Arjan Tijms
*
*/
@Stateless
public class TestService {

@PersistenceContext
private EntityManager entityManager;

public void saveNewEntity() {

TestEntity testEntity = new TestEntity();
testEntity.setValue("mytest");

entityManager.persist(testEntity);
}

public List<TestEntity> getAllEntities() {
return entityManager.createQuery("SELECT _testEntity FROM TestEntity _testEntity", TestEntity.class)
.getResultList();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">

<persistence-unit name="testPU">

<!--
This data source is defined from within the application via the @DataSourceDefinition annotation on
class org.javaee7.jpa.datasourcedefinition_annotation_pu.config.DataSourceDefinitionConfig
-->
<jta-data-source>java:app/MyApp/MyDS</jta-data-source>

<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>

</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.javaee7.jpa.datasourcedefinition_annotation_pu;

import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.inject.Inject;

import org.javaee7.jpa.datasourcedefinition_annotation_pu.config.DataSourceDefinitionConfig;
import org.javaee7.jpa.datasourcedefinition_annotation_pu.entity.TestEntity;
import org.javaee7.jpa.datasourcedefinition_annotation_pu.service.TestService;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* This tests that a data source defined via an annotation in {@link DataSourceDefinitionConfig} can be used by JPA.
* <p>
* The actual JPA code being run is not specifically relevant; any kind of JPA operation that
* uses the data source is okay here.
*
* @author Arjan Tijms
*/
@RunWith(Arquillian.class)
public class DataSourceDefinitionAnnotationPuTest {

@Inject
private TestService testService;

@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(WebArchive.class)
.addPackages(true, DataSourceDefinitionAnnotationPuTest.class.getPackage())
.addAsResource("META-INF/persistence.xml")
.addAsLibraries(Maven.resolver()
.loadPomFromFile("pom.xml")
.resolve("com.h2database:h2")
.withoutTransitivity()
.asSingleFile())
;
}

@Test
public void insertAndQueryEntity() throws Exception {

testService.saveNewEntity();

List<TestEntity> testEntities = testService.getAllEntities();

assertTrue(testEntities.size() == 1);
assertTrue(testEntities.get(0).getValue().equals("mytest"));
}
}
23 changes: 23 additions & 0 deletions jpa/datasourcedefinition-webxml-pu/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.javaee7.jpa</groupId>
<artifactId>jpa-samples</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>datasourcedefinition-webxml-pu</artifactId>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.173</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.javaee7.jpa.datasourcedefinition_webxml_pu.entity;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* A very simple JPA entity that will be used for testing
*
* @author Arjan Tijms
*
*/
@Entity
public class TestEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String value;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.javaee7.jpa.datasourcedefinition_webxml_pu.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.javaee7.jpa.datasourcedefinition_webxml_pu.entity.TestEntity;

/**
* This service does some JPA operations. The purpose of this entire test
* is just to see whether the data source can be used so the actual operations
* don't matter much.
*
* @author Arjan Tijms
*
*/
@Stateless
public class TestService {

@PersistenceContext
private EntityManager entityManager;

public void saveNewEntity() {

TestEntity testEntity = new TestEntity();
testEntity.setValue("mytest");

entityManager.persist(testEntity);
}

public List<TestEntity> getAllEntities() {
return entityManager.createQuery("SELECT _testEntity FROM TestEntity _testEntity", TestEntity.class)
.getResultList();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">

<persistence-unit name="testPU">

<!-- This data source is defined from within the application via the data-source element in web.xml -->
<jta-data-source>java:app/MyApp/MyDS</jta-data-source>

<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>

</persistence>
17 changes: 17 additions & 0 deletions jpa/datasourcedefinition-webxml-pu/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<!--
This defines the data source that's used by persistence.xml to back to the JPA persistence unit.
The database is embedded within this application (see the pom.xml of this project).
-->

<data-source>
<name>java:app/MyApp/MyDS</name>
<class-name>org.h2.jdbcx.JdbcDataSource</class-name>
<url>jdbc:h2:mem:test</url>
</data-source>

</web-app>

Loading