1, JPA entity class, Greeting, is the same as in my previous example, also included here in its entirety:
package test;2, a stateless session bean facade for CRUD operations with container-managed transaction and container-managed persistence context:
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Greeting implements Serializable {
@Id @GeneratedValue private int id;
@Basic private String message;
@Basic private String language;
public Greeting() {}
public Greeting(String message, String language) {
this.message = message;
this.language = language;
}
public String toString() {
return "Greeting id=" + id + ", message=" + message +
", language=" + language;
}
}
package test;3, TestServlet, which calls the stateless session bean:
import javax.ejb.*;
import javax.persistence.*;
@Stateless
public class GreetingBean {
@PersistenceContext
private EntityManager em;
public void create(Greeting... gs) {
for(Greeting g : gs) {
em.persist(g);
}
}
public Greeting findByLang(String lang) {
return (Greeting) em.createQuery(
"select g from Greeting g where g.language = :language")
.setParameter("language", lang).getSingleResult();
}
}
package test;4, persistence.xml with JTA transaction using a MySQL datasource:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.EJB;
@javax.servlet.annotation.WebServlet(urlPatterns = "/*")
public class TestServlet extends HttpServlet {
@EJB
private GreetingBean bean;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Greeting g_en = new Greeting("hello world", "en");
Greeting g_es = new Greeting("hola, mundo", "es");
bean.create(g_en, g_es);
out.println("Created and persisted " + g_en + ", and " + g_es);
Greeting g = bean.findByLang("en");
out.println("Query returned: " + g);
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>5, create the MySql datasource in JBoss appserver:
<persistence 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"
version="2.0">
<persistence-unit name="greeting-pu" transaction-type="JTA">
<!--
<jta-data-source>java:/DefaultDS</jta-data-source>
-->
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<!--
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
cp $JBOSS_HOME/docs/examples/jca/mysql-ds.xml $JBOSS_HOME/server/default/deploy/6, compile and package class files and persistence.xml into test.war:
# change user-name and password elements in mysql-ds.xml
cp $HOME/downloads/mysql-connector-java-5.1.15-bin.jar $JBOSS_HOME/server/default/lib/
javac -d ../ -cp "$JBOSS_HOME/client/*" *.java7, start MySql database server, JBoss default server and deploy test.war:
content of test.war:
====================
WEB-INF/classes/META-INF/persistence.xml
WEB-INF/classes/test/Greeting.class
WEB-INF/classes/test/GreetingBean.class
WEB-INF/classes/test/TestServlet.class
cd /usr/local/mysql-5.1.328, deploy and run the test:
sudo bin/mysqld_safe --user root
$JBOSS_HOME/bin/run.sh
cp test.war $JBOSS_HOME/server/default/deployIf you use the default HyperSonic embedded database in JBoss AS, instead of MySql database server, make the following adjustments:
curl http://localhost:8080/test/
(a), toggle the comments in persistence.xml;
(b), no need to copy mysql-ds.xml, or MySql driver jar;
(c), no need to start MySql database server.