/java/

JEE and Deltaspike (Wildfly 10.1.0.Final)

2017-09-20 23:36:28

/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 http:="" java.sun.com="" ns="" persistence="" persistence_2_0.xsd&quot;="" version='"2.0"' xml="" xmlns='"http://java.sun.com/xml/ns/persistence"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"'>
<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=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans beans_1_0.xsd&quot;="" http:="" java.sun.com="" javaee="" ns="" xml="" 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'>
</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&lt;&gt;();

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