【问题标题】:Configuring persistence and orm with JPA 2使用 JPA 2 配置持久性和 orm
【发布时间】:2012-12-04 12:48:43
【问题描述】:

我在我的 jBPM 项目中使用 Persistence 时遇到了一些问题。

我的配置是 jBPM 5.4 + Hibernate + JPA 2,我目前正在设置流程以通过 persistence.xml 连接到具有持久性的数据库。我只是尝试将默认数据源(在 H2 服务器中)与我的自定义 persistence.xml 连接,但我一遍又一遍地收到相同的错误:

Unknown entity: org.jbpm.persistence.processinstance.ProcessInstanceInfo

我已手动将以下内容添加到我的 src/META-INF 文件夹中的 JBPMorm-JPA2.xml,但错误仍然存​​在。谁能帮帮我?

JBPMorm-JPA2.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_2_0.xsd"
                 version="2.0">
      <named-query name="ProcessInstancesWaitingForEvent">
          <query>
select 
    processInstanceInfo.processInstanceId
from 
    ProcessInstanceInfo processInstanceInfo join processInstanceInfo.eventTypes eventTypes
where
    eventTypes = :type
          </query>
      </named-query>

      <!-- ProcessInstanceInfo mapping (needed for JPA 2) -->

      <entity class="org.jbpm.persistence.processinstance.ProcessInstanceInfo"
              metadata-complete="true">
        <pre-update method-name="update" />
        <attributes>
            <id name="processInstanceId">
                <column name="InstanceId" />
                <generated-value strategy="AUTO"/>
            </id>
            <basic name="processId" access="FIELD" />
            <basic name="startDate" access="FIELD" >
                <temporal>DATE</temporal>
            </basic>
            <basic name="lastReadDate" access="FIELD" >
                <temporal>DATE</temporal>
            </basic>
            <basic name="lastModificationDate" access="FIELD" >
                <temporal>DATE</temporal>
            </basic>
            <basic name="state" access="FIELD" />
            <basic name="processInstanceByteArray" access="FIELD" >
                <lob/>
            </basic>
            <version name="version" access="FIELD" >
                <column name="OPTLOCK" />
            </version>
            <element-collection name="eventTypes" target-class="java.lang.String" access="FIELD" >
                <collection-table name="EventTypes">
                    <join-column name="InstanceId"/>
                </collection-table> 
            </element-collection>
            <transient name="processInstance" />
            <transient name="env" />
        </attributes>
      </entity>

</entity-mappings>

persistence.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence
  version="1.0"
  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"
  xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/persistence">

  <persistence-unit name="IALPR" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/jbpm-ds</jta-data-source>
    <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.dialect" value="org.hibernate.dialect.H2Dialect"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.BTMTransactionManagerLookup"/>

    </properties>

  </persistence-unit>

</persistence>

更新:

为了解决这个问题,在 META-INF 文件夹中创建一个 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>

如果有人知道为 jBPM5 配置持久性的好教程,请分享...这太疯狂了!

【问题讨论】:

    标签: hibernate persistence jpa-2.0 jbpm


    【解决方案1】:

    好的,这里有一个使用 MySQL 数据库和 JBoss AS 在 JBPM 中配置持久性的小教程:

    1) 在你的 src/main/java 文件夹下创建一个 META-INF 文件夹

    2) 创建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) 创建 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) 创建 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) 现在您必须定义您的数据源。我使用 JBoss5,这个版本的 JBoss 将读取任何带有 *-ds.xml 模式的文件作为数据源的定义。你必须把这个文件放在你的部署文件夹中(你可能会注意到那里已经有一个数据源文件,但不会有冲突)。如果您使用的是 JBoss7,则有另一种定义 DS 的方法 - 我想这可能会有所帮助 https://community.jboss.org/wiki/DataSourceConfigurationInAS7

    无论如何,yourDS-ds.xml 应该是这样的:

    <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) 以上指令至少足以在数据库中创建持久化表。当您最终开始在 JBPM 中使用任务时,可能需要创建一个 Taskorm.xml 文件(google 一下,它太长了)。我不确定是否有必要,但无论如何我都有。

    7) 最后,只需通过 EntityManagerFactory 在 Java 中调用您的持久性单元,创建您的环境并启动一个新会话。持久性数据应自动保存到数据库中。

    希望这对您有所帮助。干杯!

    【讨论】:

    • 我去度假了,抱歉没能早点回复。您将 orm.xml 文件保存在 src/main/java 文件夹下的 META-INF 文件夹中
    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 2013-05-28
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多