JEE and Deltaspike (Wildfly 10.1.0.Final)

/src/main/java/pl/btbw/post/PostRepository.java

package pl.btbw.post;

import org.apache.deltaspike.data.api.EntityRepository;
import org.apache.deltaspike.data.api.Repository;

@Repository
public interface PostRepository extends EntityRepository<Post, Long> {
}

/src/main/java/pl/btbw/core/EntityManagerProducer.java

package pl.btbw.core;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@ApplicationScoped
public class EntityManagerProducer {

	@PersistenceContext(unitName = "ExamplePg")
	private EntityManager entityManager;

	@Produces
	@Default
	@RequestScoped
	public EntityManager create() {
		return this.entityManager;
	}

}

/src/main/resources/META-INF/persistence.xml

<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="ExamplePg" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/ExamplePgDS</jta-data-source>
    </persistence-unit>

</persistence>

/src/main/resources/META-INF/apache-deltaspike.properties

globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy

/src/main/webapp/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

/src/main/java/pl/btbw/core/MyApplication.java

package pl.btbw.core;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class MyApplication extends Application {
}

/src/main/java/pl/btbw/post/Post.java

package pl.btbw.post;

import javax.persistence.*;

@Entity
@Table(name = "post")
@NamedQueries({
		@NamedQuery(name = "Post.all", query = "SELECT p FROM Post p")
})
public class Post {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;

	public Post() {
	}

	public Post(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

/src/main/java/pl/btbw/post/PostController.java

package pl.btbw.post;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/")
public class PostController {

	@Inject
	private PostRepository postRepository;

	@GET
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	public List<PostDto> doAll() {

		List<Post> posts = postRepository.findAll();

		List<PostDto> dtos = new ArrayList<>();

		for (Post post : posts) {
			dtos.add(new PostDto(post.getId(), post.getName()));
		}

		return dtos;
	}

	@GET
	@Path("/{name}")
	@Produces(MediaType.APPLICATION_JSON)
	public Map<String, String> doAdd(@PathParam("name") String name) {

		postRepository.saveAndFlush(new Post(name));

		return new HashMap<String, String>() {{
			put("message", "ok");
		}};
	}
}

/src/main/java/pl/btbw/post/PostDto.java

package pl.btbw.post;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class PostDto {

	@XmlElement
	private int id;

	@XmlElement
	private String name;

	public PostDto(int id, String name) {
		this.id = id;
		this.name = name;
	}
}