Add runtime Websphere application server 7 to the build path
Under src, create a folder META-INF, under META-INF create persistence.xml
The contents of persistence.xml
<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_1_0.xsd" version="1.0"> <persistence-unit name="unit"> <jta-data-source>jdbc/derby</jta-data-source> <properties> <property name="openjpa.Log" value="SQL=TRACE"/> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> <property name="openjpa.jdbc.MappingDefaults" value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict"/> </properties> </persistence-unit> </persistence>
jdbc/derby is the JNDI name for the datasource. It should be configured in Websphere application server already.
Openjpa.Log prints out the SQL. (Not working properly in WAS 7)
Openjpa.jdbc.synchronizeMappings creates the tables in the database if they don’t already exist.
openjpa.jdbc.MappingDefaults creates the foreign key constraints.
Create an entity bean: Person.java
package entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_PERSON") public class Person{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name = "NAME") private String name; public Person() { super(); } public Person(String name) { super(); this.name = name; } public Person(int id, String name) { super(); this.id = id; 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; } }
Create a stateless session bean and its local interface: PersonManager.java and PersonManagerBean.java
package session; import entity.Person; public interface PersonManager { public void save(Person person); } import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import entity.Person; @Stateless @Local(PersonManager.class) public class PersonManagerBean implements PersonManager { @PersistenceContext(unitName="unit") private EntityManager em; @Override public void save(Person person) { em.persist(person); } }
Create a dynamic web project, named ‘ejb3-jpa-web’
Select ‘Websphere Application Server 7’ as the Target Runtime.
Include ‘ejb3-jpa’ project into the build path
Create a servlet: PersonServlet.java
package web; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import session.PersonManager; import entity.Person; public class PersonServlet extends HttpServlet { @EJB private PersonManager personManager; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { personManager.save(new Person("Sun")); } }
Define the servlet in web.xml
<servlet> <servlet-name>person</servlet-name> <servlet-class>web.PersonServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>person</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
Create an enterprise project, named ‘ejb3-ear’
Add ‘ejb3-jpa’ and ‘ejb3-jpa-web’ project into ‘ejb3-ear’
Add the ‘ejb3-ear’ project into the server, start the server
Right click on ‘WebSphere Application Server V7.0’, and click on ‘Properties’
Go to http://localhost:PORT_NUMBER/ejb3-jpa-web/a.do
Replace PORT_NUMBER with the actual port number (i.e. 9084)
Check the database