Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding demo showing how to use CDI + JPA + JTA (@Transactional, trans… #39

Merged
merged 2 commits into from
Jan 22, 2019
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ transaction.log
infinispan.log

.gradle

#Misc.
ObjectStore
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ This repository contains demos used live during presentations or in blog posts;
Accompanies the blog post http://in.relation.to/2018/02/26/putting-bean-validation-constraints-to-multimaps/

* Java 9
- multi-release-jar-demo: Shows how to build multi-release JARs with Java 9.
- _multi-release-jar-demo_: Shows how to build multi-release JARs with Java 9.
Accompanies the blog post http://in.relation.to/2017/02/13/building-multi-release-jars-with-maven/
- custom-jlink-plugin: Shows how to customize Java 9 modular runtime images with jlink plug-ins. The example shows a plug-in for adding a Jandex annotation index for one or more modules to the runtime image.
- _custom-jlink-plugin_: Shows how to customize Java 9 modular runtime images with jlink plug-ins. The example shows a plug-in for adding a Jandex annotation index for one or more modules to the runtime image.
* Other
- wildfly-patch-creation: How to create WildFly patch files.
- _wildfly-patch-creation_: How to create WildFly patch files.
Accompanies the blog post http://in.relation.to/2017/05/29/creating-patches-for-wildfly/
- _cdi-jpa-testing_: How to run JUnit tests using CDI, JPA and JTA

## License

Expand Down
180 changes: 180 additions & 0 deletions other/cdi-jpa-testing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
License: Apache License, Version 2.0
See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
-->

<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>

<groupId>org.hibernate.demos</groupId>
<artifactId>jpa-cdi-testing</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Demo for testing CDI + JPA + JTA under Java SE</name>
<url>https://hibernate.org/orm</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

<version.com.h2database>1.4.197</version.com.h2database>
<version.org.hibernate>5.4.0.Final</version.org.hibernate>
<version.narayana>5.9.2.Final</version.narayana>
<version.jnpserver>5.0.3.GA</version.jnpserver>
<version.jboss-transaction-api>1.1.1.Final</version.jboss-transaction-api>
<version.log4j>1.2.17</version.log4j>
<version.weld>3.0.5.Final</version.weld>
</properties>

<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0.SP1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<version>${version.jboss-transaction-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${version.org.hibernate}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${version.com.h2database}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>${version.weld}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.module</groupId>
<artifactId>weld-jta</artifactId>
<version>${version.weld}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta</artifactId>
<version>${version.narayana}</version>
<exclusions>
<exclusion>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${version.log4j}</version>
</dependency>
<dependency>
<groupId>org.jboss.naming</groupId>
<artifactId>jnpserver</artifactId>
<version>${version.jnpserver}</version>
<exclusions>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-spi</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit4</artifactId>
<version>1.3.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>

</build>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class GreetingService {

public String greet(String name) {
return "Hello, " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.event.TransactionPhase;
import javax.inject.Inject;
import javax.transaction.Transactional;

@ApplicationScoped
public class ObserverTestBean {

@Inject
private Event<Long> event1;

@Inject
private Event<Integer> event2;

@Inject
private Event<Short> event3;

private final StringBuilder sb = new StringBuilder();

@Transactional
public void work() {
event1.fire(1L);
event2.fire(2);
event3.fire((short) 3);
}

public void observesLong(@Observes(during=TransactionPhase.AFTER_COMPLETION) Long event) {
sb.append(event);
}

public void observeInteger(@Observes(during=TransactionPhase.BEFORE_COMPLETION) Integer event) {
sb.append(event);
}

public void observesShort(@Observes(during=TransactionPhase.IN_PROGRESS) Short event) {
sb.append(event);
}

public String getResult() {
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;

@Entity
@EntityListeners(TestListener.class)
public class TestEntity {

@Id
public UUID id;

public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.PostPersist;

@ApplicationScoped
public class TestListener {

@Inject
private TestService testService;

@PostPersist public void onPostPersist(TestEntity entity) {
testService.addTestEntityName(entity.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;

@ApplicationScoped
public class TestService {

private final List<String> names = new ArrayList<>();
private final EntityManager entityManager;

@Inject
public TestService(EntityManager entityManager) {
this.entityManager = entityManager;
}

public List<String> getTestEntityNames() {
return names;
}

public void addTestEntityName(String name) {
names.add(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* License: Apache License, Version 2.0
* See the LICENSE file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.demos.jpacditesting;

import javax.enterprise.context.ApplicationScoped;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

@ApplicationScoped
public class TransactionalTestService {

@Transactional(value=TxType.MANDATORY)
public String doSomething() {
return "Success";
}
}
Empty file.
Loading