日系框架之seasar2(S2JPA之OpenJPA)
MySql上新建資料庫s2openjpa,並執行如下DDL:
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for dept
- -- ----------------------------
- CREATE TABLE `dept` (
- `deptno` int(11) NOT NULL,
- `active` bit(1) NOT NULL,
- `dname` varchar(255) default NULL,
- `loc` varchar(255) default NULL,
- `versionNo` int(11) NOT NULL,
- PRIMARY KEY (`deptno`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- -- ----------------------------
- -- Table structure for emp
- -- ----------------------------
- CREATE TABLE `emp` (
- `empno` bigint(20) NOT NULL,
- `city` varchar(255) default NULL,
- `zip` varchar(255) default NULL,
- `comm` float default NULL,
- `ename` varchar(255) default NULL,
- `hiredate` date default NULL,
- `job` varchar(255) default NULL,
- `mgr` smallint(6) default NULL,
- `sal` float default NULL,
- `tstamp` datetime default NULL,
- `deptno` int(11) default NULL,
- PRIMARY KEY (`empno`),
- KEY `FK110A8F5074ABE` (`deptno`),
- CONSTRAINT `FK110A8F5074ABE` FOREIGN KEY (`deptno`) REFERENCES `dept` (`deptno`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- -- ----------------------------
- -- Records
- -- ----------------------------
- INSERT INTO `dept` VALUES ('10', '', 'ACCOUNTING', 'NEW YORK', '0');
- INSERT INTO `dept` VALUES ('20', '', 'RESEARCH', 'DALLAS', '0');
- INSERT INTO `dept` VALUES ('30', '', 'SALES', 'CHICAGO', '0');
- INSERT INTO `dept` VALUES ('40', '', 'OPERATIONS', 'BOSTON', '0');
- INSERT INTO `emp` VALUES ('7369', 'LAS VEGAS', '110', null, 'SMITH', '1980-12-17', 'CLERK', '7902', '800', '2000-01-01 00:00:00', '20');
- INSERT INTO `emp` VALUES ('7499', 'MIAMI', '120', '300', 'ALLEN', '1981-02-20', 'SALESMAN', '7698', '1600', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7521', 'WASHINGTON', '130', '500', 'WARD', '1981-02-22', 'SALESMAN', '7698', '1250', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7566', 'IOWA CITY', '140', null, 'JONES', '1981-04-02', 'MANAGER', '7839', '2975', '2000-01-01 00:00:00', '20');
- INSERT INTO `emp` VALUES ('7654', 'LOS ANGELES', '150', '1400', 'MARTIN', '1981-09-28', 'SALESMAN', '7698', '1250', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7698', 'LOS ANGELES', '160', null, 'BLAKE', '1981-05-01', 'MANAGER', '7839', '2850', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7782', 'WASHINGTON', '170', null, 'CLARK', '1981-06-09', 'MANAGER', '7839', '2450', '2000-01-01 00:00:00', '10');
- INSERT INTO `emp` VALUES ('7788', 'NEW YORK', '180', null, 'SCOTT', '1982-12-09', 'ANALYST', '7566', '3000', '2005-01-18 13:09:32', '20');
- INSERT INTO `emp` VALUES ('7839', 'SEATTLE', '190', null, 'KING', '1981-11-17', 'PRESIDENT', null, '5000', '2000-01-01 00:00:00', '10');
- INSERT INTO `emp` VALUES ('7844', 'NEW YORK', '210', '0', 'TURNER', '1981-09-08', 'SALESMAN', '7698', '1500', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7876', 'SANTA FE', '220', null, 'ADAMS', '1983-01-12', 'CLERK', '7788', '1100', '2000-01-01 00:00:00', '20');
- INSERT INTO `emp` VALUES ('7900', 'NEW YORK', '230', null, 'JAMES', '1981-12-03', 'CLERK', '7698', '950', '2000-01-01 00:00:00', '30');
- INSERT INTO `emp` VALUES ('7902', 'SANTA FE', '240', null, 'FORD', '1981-12-03', 'ANALYST', '7566', '3000', '2000-01-01 00:00:00', '20');
- INSERT INTO `emp` VALUES ('7934', 'IOWA CITY', '250', null, 'MILLER', '1982-01-23', 'CLERK', '7782', '1300', '2000-01-01 00:00:00', '10');
變更jar包一覽如下:
aopalliance-1.0.jar
commons-collections-3.2.jar
commons-lang-2.1.jar
commons-logging-1.1.jar
commons-pool-1.3.jar
ejb3-persistence.jar
geronimo-annotation_1.0_spec-1.0.jar
geronimo-ejb_3.0_spec-1.0.jar
geronimo-interceptor_3.0_spec-1.0.jar
geronimo-j2ee_1.4_spec-1.0.jar
javassist-3.4.GA.jar
jboss-archive-browsing-5.0.0alpha-200607201-119.jar
jta-1.1.jar
junit-3.8.2.jar
log4j-1.2.13.jar
mysql-connector-java-5.1.6-bin.jar
ognl-2.6.9-patch-20070908.jar
openjpa-1.1.0.jar
poi-3.0-FINAL.jar
s2-extension-2.4.29.jar
s2-framework-2.4.29.jar
s2openjpa-1.0.0.jar
s2-tiger-2.4.29.jar
serp-1.13.1.jar
修改jdbc.dicon
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
- "http://www.seasar.org/dtd/components24.dtd">
- <componentsnamespace="jdbc">
- <includepath="jta.dicon"/>
- <includepath="jdbc-extension.dicon"/>
- <componentclass="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
- <componentclass="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory">
- <arg>
- <componentclass="org.seasar.extension.jdbc.impl.BasicStatementFactory"/>
- </arg>
- <propertyname="fetchSize">100</property>
- </component>
- <componentname="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <propertyname="driverClassName">
- "com.mysql.jdbc.Driver"
- </property>
- <propertyname="URL">
- "jdbc:mysql://localhost:3306/s2openjpa"
- </property>
- <propertyname="user">"root"</property>
- <propertyname="password">"root"</property>
- </component>
- <componentname="connectionPool"
- class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
- <propertyname="timeout">600</property>
- <propertyname="maxPoolSize">10</property>
- <propertyname="allowLocalTx">true</property>
- <destroyMethodname="close"/>
- </component>
- <componentname="dataSource"class="org.seasar.extension.dbcp.impl.DataSourceImpl"/>
- </components>
jpa.dicon檔案:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
- "http://www.seasar.org/dtd/components24.dtd">
- <componentsinitializeOnCreate="true">
- <includepath="s2openjpa.dicon"/>
- <componentname="persistenceUnitProvider"class="org.seasar.framework.jpa.impl.ContainerPersistenceUnitProvider">
- <propertyname="unitName">"s2openjpaUnit"</property>
- <propertyname="providerClassName">"org.apache.openjpa.persistence.PersistenceProviderImpl"</property>
- </component>
- <componentname="entityManagerFactory"class="javax.persistence.EntityManagerFactory">
- persistenceUnitProvider.entityManagerFactory
- </component>
- <componentname="entityManager"class="org.seasar.framework.jpa.impl.TxScopedEntityManagerProxy"/>
- </components>
persistence.xml:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <persistencexmlns="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-unitname="s2openjpaUnit"transaction-type="RESOURCE_LOCAL">
- <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
- <jta-data-source>jdbc/dataSource</jta-data-source>
- <class>examples.entity.Department</class>
- <class>examples.entity.Employee</class>
- <properties>
- <propertyname="openjpa.ManagedRuntime"value="org.seasar.openjpa.ee.S2ManagedRuntime"/>
- <propertyname="openjpa.jdbc.DBDictionary"value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
- <propertyname="openjpa.Log"value="DefaultLevel=WARN, Enhance=ERROR, SQL=TRACE"/>
- </properties>
- </persistence-unit>
- </persistence>
修改DepartmentDaoImpl.java
- package examples.dao;
- import javax.ejb.Stateless;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- import examples.entity.Department;
- @Stateless
- publicclass DepartmentDaoImpl implements DepartmentDao {
- @PersistenceContext(unitName = "s2openjpaUnit")
- EntityManager em;
- public DepartmentDaoImpl() {
- }
- public Department getDepartment(int id) {
- return em.find(Department.class, id);
- }
- }
其他檔案無需修改。
測試程式碼部分:
DepartmentClient.java無需修改。
EntityManagerFactoryClient.java
- package examples.work;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import org.seasar.framework.container.S2Container;
- import org.seasar.framework.container.factory.S2ContainerFactory;
- import org.seasar.framework.jpa.PersistenceUnitManager;
- import examples.entity.Department;
- publicclass EntityManagerFactoryClient {
- privatestaticfinal String PATH = "app.dicon";
- publicstaticvoid main(String[] args) {
- S2Container container = S2ContainerFactory.create(PATH);
- container.init();
- try {
- EntityManagerFactory emf = (EntityManagerFactory) container.getComponent(EntityManagerFactory.class);
- EntityManager em = emf.createEntityManager();
- Department department = (Department) em.find(Department.class, 10);
- System.out.println(department.getDeptno());
- PersistenceUnitManager pum = PersistenceUnitManager.class.cast(container.getComponent(PersistenceUnitManager.class));
- emf = pum.getEntityManagerFactory("s2openjpaUnit");
- em = emf.createEntityManager();
- department = (Department) em.find(Department.class, 10);
- System.out.println(department.getDeptno());
- } finally {
- container.destroy();
- }
- }
- }
- package examples.work;
- import java.util.List;
- import javax.ejb.TransactionAttribute;
- import javax.ejb.TransactionAttributeType;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.Query;
- import org.seasar.framework.container.S2Container;
- import org.seasar.framework.container.factory.S2ContainerFactory;
- import examples.entity.Department;
- publicclass EntityManagerClient {
- privatestaticfinal String PATH = "app.dicon";
- publicstaticvoid main(String[] args) {
- S2Container container = S2ContainerFactory.create(PATH);
- container.init();
- try {
- EntityManagerFactory emf = (EntityManagerFactory) container.getComponent(EntityManagerFactory.class);
- EntityManager em = emf.createEntityManager();
- Department department = (Department)em.find(Department.class, 10);
- updateDepartment(em, department);
- Query query = em.createQuery("select d from Dept d where d.deptno = 10");
- List<?> list = query.getResultList();
- for (int i = 0; i < list.size(); i++) {
- System.out.println(((Department)list.get(i)).getDname());
- }
- } finally {
- container.destroy();
- }
- }
- @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
- publicstaticvoid updateDepartment(EntityManager em, Department department){
- System.out.println(department.getDname());
- department.setDname(department.getDname() + "XXX");
- EntityTransaction tx = em.getTransaction();
- tx.begin();
- em.persist(department);
- em.flush();
- tx.commit();
- }
- }
關於OpenJPA(注意版本,這裡使用1.1版):