This document is for S2Hibernate-JPA 1.0.1
S2Hibernate-JPA has the following features.
- Import project
S2Hibernate-JPA requires JDK5 or later.
Download Seasar2 in advance and import it into workspace as a java project in Eclipse.
Import the s2hibernate-jpa directory extracted from S2Hibernate-JPA-x.x.x.zip into workspace as a java project in Eclipse.
- Required jar file
Files necessary to use S2Hibernate-JPA are included in the s2hibernate/lib directory. Also, Seasar2 jar files are required.
When some connection pooling or cache implementation not included in S2Hibernate3 is required, download it from Hibernate.org.
- Classpath
To use S2Hibernate-JPA, add to the CLASSPATH *.jar files under the lib directory (except h2-1.0.20070429.jar) and resources(j2ee.dicon, s2hibernate-jpa.dicon, log4j.properties, META-INF/persistence.xml). If S2Hibernate-JPA files are imported to an Eclipse workspace, it is not necessary to add these files to the CLASSPATH.
- Database
H2 Database Engine is included to try out S2Hibernate-JPA. h2-1.0.20070429.jar is necessary to execute H2 Database Engine but is unnecessary in a production environment which does not use H2 Database Engine.
To apply the S2Hibernate to the project, entity, Dao(.java) and dicon files are required.
Create entities following the Persistence API specifications.
Dao implementation
- Define the EntityManager type field.
private EntityManager entityManager;
- Define the setter method or constructor to accept the implementation object.
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
- Use the EntityManager in each method.
public Employee getEmployee(int empno) {
return entityManager.find(Employee.class, 7788);
}
incluede j2ee.dicon and s2hibernate-jpa.dicon.
Create the 'resources/META-INF' in the CLASSPATH directory. In the META-INF directory, place the persistence.xml as follows.
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="em" transaction-type="JTA">
<jta-data-source>j2ee.dataSource</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.jndi.class"
value="org.seasar.extension.j2ee.JndiContextFactory"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.seasar.hibernate.jpa.transaction.S2TransactionManagerLookup"/>
<property name="hibernate.show_sql"
value="false"/>
<property name="hibernate.format_sql"
value="true"/>
<property name="hibernate.use_sql_comments"
value="false"/>
</properties>
</persistence-unit>
</persistence>
|