Skip to content
Permalink
Browse files

Added JPA21 example

  • Loading branch information...
lincolnthree committed Sep 9, 2013
1 parent 06ab953 commit 9749b9e94f51a3c3bbdca144ed5c5637cc854f1a
@@ -0,0 +1,42 @@
<?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>
<groupId>org.javaee7.samples.jpa</groupId>
<artifactId>jpa-standard</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>JPA Standard</name>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

@@ -0,0 +1,56 @@
package org.javaee7.samples.jta;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name = "countries")
public class Country
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Version
private Long version;

private String name;

/**
* Getters and Setters
*/
public Integer getId()
{
return id;
}

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

public Long getVersion()
{
return version;
}

public void setVersion(Long version)
{
this.version = version;
}

public String getName()
{
return name;
}

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

}
@@ -0,0 +1,73 @@
package org.javaee7.samples.jta;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(indexes = {
@Index(columnList = "name"),
@Index(columnList = "id")
})
public class Customer
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Version
private Long version;

private String name;

@ManyToOne
private Country country;

/**
* Getters and Setters
*/
public Integer getId()
{
return id;
}

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

public Long getVersion()
{
return version;
}

public void setVersion(Long version)
{
this.version = version;
}

public String getName()
{
return name;
}

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

public Country getCountry()
{
return country;
}

public void setCountry(Country country)
{
this.country = country;
}
}
@@ -0,0 +1,77 @@
package org.javaee7.samples.jta;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@WebServlet(urlPatterns = { "/" })
public class TestServlet extends HttpServlet
{
private static final long serialVersionUID = -6642422622622245892L;

@PersistenceContext
private EntityManager em;

private static int count = 0;

@PostConstruct
public void initNamedQuery()
{
em.getEntityManagerFactory().addNamedQuery("customersOrderByName",
em.createQuery("from Customer c ORDER BY c.name DESC"));
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>JDBC Connection Resource</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>JDBC Connection Resource</h1>");

Customer customer = new Customer();
customer.setName("Person " + count);
em.persist(customer);
em.flush();

List<Customer> customers = em.createNamedQuery("customersOrderByName", Customer.class).getResultList();
Iterator<Customer> iterator = customers.iterator();

out.println("<ul>");
while (iterator.hasNext())
{
out.println("<li>" + iterator.next().getName() + "</li>");
}
out.println("</ul>");

out.println("</body>");
out.println("</html>");
}
}

@Override
public String getServletInfo()
{
return "JDBC Example Servlet";
}
}
@@ -0,0 +1,9 @@
<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://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="default" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>

</persistence>

0 comments on commit 9749b9e

Please sign in to comment.
You can’t perform that action at this time.