/java/

Wildfly 8.1.0.Final and Hibernate

2014-09-18 14:46:04

http://wildfly.org/ http://hibernate.org/ https://github.com/szalek/Jee-hibernate

Database configuration

./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='"ExamplePgDS"' transaction-type='"JTA"'>
<jta-data-source>java:jboss/datasources/ExamplePgDS</jta-data-source>
</persistence-unit>
</persistence>

Jboss PostgreSQL configuration

...

<datasource enabled='"true"' jndi-name='"java:jboss/datasources/ExamplePgDS"' jta='"true"' pool-name='"ExamplePgDS"' use-ccm='"true"' use-java-context='"true"'>
<connection-url>jdbc:postgresql://localhost:5432/test</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql-jdbc4</driver>
<pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
<validation>
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
</validation>
</datasource>

...

<driver module='"org.postgresql"' name='"postgresql-jdbc4"'>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>

Application configuration

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

Web part, controller and dto

PostController.java

package pl.btbw.web;

import pl.btbw.persistent.Post;
import pl.btbw.persistent.PostRepository;

import javax.ejb.Stateless;
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;

@Stateless
@Path("/")
public class PostController {

    @Inject
    private PostRepository postRepository;

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public List

<postdto> doAll() {

        List<post> posts = postRepository.all();

        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.save(new Post(name));

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

PostDto.java

package pl.btbw.web;

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

Repository part

PostRepository.java

package pl.btbw.persistent;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Stateless
@LocalBean
public class PostRepository {

    @PersistenceContext(unitName = "ExamplePgDS")
    private EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void save(Post post) {
        em.persist(post);
    }

    public List<post> all() {
        Query query = em.createNamedQuery("Post.all");
        return (List<post>) query.getResultList();
    }

}

Post.java

package pl.btbw.persistent;

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