Transaction is managed by embeddable EJB container. The persistence unit is backed by an application-scoped JTA DataSource declared with annotation on EJB class. No need to package or deploy anything. Client and server pieces are all in a single JVM.
classes/META-INF/persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>JPA entity Employee:
<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="test_pu" transaction-type="JTA">
<jta-data-source>java:app/jdbc/test</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
package test;No-interface stateless EJB:
import javax.persistence.*;
@Entity
public class Employee implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Employee)) {
return false;
}
Employee other = (Employee) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override public String toString() {
return "Employee id=" + id + ", name=" + name;
}
}
package test;main class:
import javax.ejb.Stateless;
import javax.persistence.*;
import javax.annotation.sql.DataSourceDefinition;
@Stateless
@DataSourceDefinition(
name="java:app/jdbc/test",
className="com.mysql.jdbc.jdbc2.optional.MysqlDataSource",
user="root",
password="root",
databaseName="test",
serverName="localhost",
portNumber=3306 )
public class TestBean {
@PersistenceContext private EntityManager em;
public void addEmployee(String[] names) {
for(String name : names) {
Employee e = new Employee();
e.setName(name);
em.persist(e);
}
}
}
package test;To compile from project directory:
import javax.ejb.embeddable.EJBContainer;
import javax.naming.*;
public class Client {
public static void main(String[] args) throws Exception {
EJBContainer container = EJBContainer.createEJBContainer();
Context namingContext = container.getContext();
TestBean testBean = (TestBean) namingContext.lookup("java:global/classes/TestBean");
testBean.addEmployee(args); //need to pass in employee names from command line
container.close();
}
}
$ javac -cp "classes:$GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar" classes/test/*javaStart mysqld, and run the app:
cd /usr/local/mysql-5.1.32; sudo bin/mysqld_safe --user rootVerify entities are successfully created by running mysql:
java -cp "classes:$GLASSFISH_HOME/lib/embedded/glassfish-embedded-static-shell.jar:$HOME/mysql-connector-java-5.1.5-bin.jar" test.Client Jon Jane
mysql> select * from EMPLOYEE;If you are wondering why the jar is named xxx-static-shell.jar, I guess "static" means it's not OSGi bundle, and "shell" means it's a wrapper for all the other GlassFish jars.
+----+------+
| ID | NAME |
+----+------+
| 1 | Jon |
| 2 | Jane |
+----+------+
2 rows in set (0.00 sec)