您的位置:首页 > 产品设计 > 产品经理

Configuring persistence and orm with JPA 2

2014-01-09 17:39 645 查看
摘要: jBPM 5.4 + Hibernate + JPA 2

Ok, so here goes a little tutorial to configure persistence in JBPM, using a MySQL database and JBoss AS:

1) Create a META-INF folder under your src/main/java folder

2) Create persistence.xml

<?xml version="1.0" encoding="UTF-8"?><persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
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 http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"> <persistence-unit name="your_unit_name" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/your_data_source_name</jta-data-source>
<mapping-file>META-INF/JBPMorm.xml</mapping-file>
<mapping-file>META-INF/ProcessInstanceInfo.hbm.xml</mapping-file>
<!-- The tables that will be created in your specified sql schema -->
<class>org.drools.persistence.info.SessionInfo</class>
<class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
<class>org.drools.persistence.info.WorkItemInfo</class>
<properties>
<property name="hibernate.default_schema" value="your_schema_name" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.connection.autocommit" value="false" />
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>

3) Create orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns=" http://java.sun.com/xml/ns/persistence/orm "    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "    xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"    version="1.0">
<named-query name="ProcessInstancesWaitingForEvent">
<query>
select processInstanceInfo.processInstanceId from ProcessInstanceInfo processInstanceInfo where :type in elements(processInstanceInfo.eventTypes)
</query>
</named-query>
</entity-mappings>

4) Create ProcessInstanceInfo.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="org.jbpm.persistence.processinstance">     <!-- access="field" for fields that have no setter methods -->     <class name="ProcessInstanceInfo" table="ProcessInstanceInfo">         <id name="processInstanceId" type="long" column="InstanceId">             <generator class="native" />        </id>        <version name="version" type="integer" unsaved-value="null" access="field">          <column name="OPTLOCK" not-null="false" />        </version>        <property name="processId" access="field" />        <property name="startDate" type="timestamp" access="field" />        <property name="lastReadDate" type="timestamp"  access="field" />        <property name="lastModificationDate" type="timestamp" access="field" />        <property name="state" type="integer" not-null="true" access="field" />       <property name="processInstanceByteArray" type="org.hibernate.type.PrimitiveByteArrayBlobType"            column="processInstanceByteArray" access="field" length="2147483647" />        <set name="eventTypes" table="EventTypes" access="field" >            <key column="InstanceId"/>            <element column="element" type="string"/>        </set>        <!-- NOT mapping [processInstance] field because field is transient -->            <!-- NOT mapping [env] field because field is transient -->        </class></hibernate-mapping>

5) Now you have to define your datasource. I use JBoss5, and this version of JBoss will read any file with the pattern *-ds.xml as being the definition of your datasource. You have to put this file in your deploy folder (and you may notice there's already a datasource file there, but there will be no conflicts). If you're using JBoss7, there's a different way to define the DS - I suppose this might be helpfulhttps://community.jboss.org/wiki/DataSourceConfigurationInAS7.

Anyway, here's what your yourDS-ds.xml should look like:

<datasources> <local-tx-datasource> <jndi-name>jdbc/your_datasource_name</jndi-name> <connection-url>your_db_url</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>your_user</user-name> <password>your_pass</password> <min-pool-size>5</min-pool-size> <max-pool-size>20</max-pool-size> <idle-timeout-minutes>5</idle-timeout-minutes> </local-tx-datasource></datasources>


6) The above instructions are enough to at least create the persistence tables in the database. When you eventually start using tasks in JBPM, it may be required to create a Taskorm.xml file (google it, it's too long). I'm not sure if it's necessary, but I have it anyway.

7) Finally, just call your persistence unit in Java through the EntityManagerFactory, create your environment and start a new session. The persistence data should be automatically saved to the DB.

Hope this was helpful. Cheers!

3ff0

share|improve this answer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息