使用JPA 2配置持久性和orm [英] Configuring persistence and orm with JPA 2
问题描述
我的配置是jBPM 5.4 + Hibernate + JPA 2,我目前正在设置流程通过persistence.xml连接到具有持久性的数据库。我只是试图将默认的数据源(在H2服务器中)与我的自定义persistence.xml连接起来,但我一直得到同样的错误:
未知实体:org.jbpm.persistence.processinstance.ProcessInstanceInfo
我已经手动将以下内容添加到我的src / META-INF文件夹JBPMorm-JPA2.xml中,但错误仍然存在。任何人都可以帮助我吗?
JBPMorm-JPA2.xml:
<?xml version =1.0encoding =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
< / named-query>
<! - ProcessInstanceInfo映射(JPA 2需要) - >
< entity class =org.jbpm.persistence.processinstance.ProcessInstanceInfo
metadata-complete =true>
< pre-update method-name =update/>
<属性>
< id name =processInstanceId>
< column name =InstanceId/>
< generated-value strategy =AUTO/>
< / id>
< basic name =processIdaccess =FIELD/>
< basic name =startDateaccess =FIELD>
< temporal> DATE< / temporal>
< / basic>
< basic name =lastReadDateaccess =FIELD>
< temporal> DATE< / temporal>
< / basic>
< basic name =lastModificationDateaccess =FIELD>
< temporal> DATE< / temporal>
< / basic>
< basic name =stateaccess =FIELD/>
< basic name =processInstanceByteArrayaccess =FIELD>
< lob />
< / basic>
< version name =versionaccess =FIELD>
< column name =OPTLOCK/>
< / version>
< element-collection name =eventTypestarget-class =java.lang.Stringaccess =FIELD>
< collection-table name =EventTypes>
< join-column name =InstanceId/>
< / collection-table>
< / element-collection>
<临时名称=processInstance/>
<瞬间名称=env/>
< / attributes>
< / entity>
< / entity-mappings>
persistence.xml:
<?xml version =1.0encoding =UTF-8standalone =yes?>
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 =IALPRtransaction-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>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect/>
< property name =hibernate.max_fetch_depthvalue =3/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.transaction.manager_lookup_class
value =org.hibernate.transaction.BTMTransactionManagerLookup/>
< / properties>
< / persistence-unit>
< /持久性>
更新:
为了解决这个问题,在META-INF文件夹中创建一个ProcessInstanceInfo.hbm.xml文件,其中包含以下内容:
<?xml version =1.0encoding =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表示没有setter方法的字段 - >
< class name =ProcessInstanceInfotable =ProcessInstanceInfo>
< id name =processInstanceIdtype =longcolumn =InstanceId>
< generator class =native/>
< / id>
< version name =versiontype =integerunsaved-value =nullaccess =field>
< column name =OPTLOCKnot-null =false/>
< / version>
< property name =processIdaccess =field/>
< property name =startDatetype =timestampaccess =field/>
< property name =lastReadDatetype =timestampaccess =field/>
< property name =lastModificationDatetype =timestampaccess =field/>
< property name =processInstanceByteArraytype =org.hibernate.type.PrimitiveByteArrayBlobType
column =processInstanceByteArrayaccess =fieldlength =2147483647/>
< set name =eventTypestable =EventTypesaccess =field>
< key column =InstanceId/>
< element column =elementtype =string/>
< / set>
<! - 不映射[processInstance]字段,因为字段是暂时的 - >
<! - 不映射[env]字段,因为字段是瞬态的 - >
< / class>
< / hibernate-mapping>
如果有人知道配置jBPM5持久性的好教程,请分享......这是疯了!
好的,下面介绍一些使用MySQL数据库和JBoss AS配置JBPM持久性的教程:
$ b $ 1)在src / main / java文件夹下创建一个META-INF文件夹
$ b
2)创建persistence.xml
<?xml version =1.0encoding =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_nametransaction-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>
<映射文件> META-INF / ProcessInstanceInfo.hbm.xml< /映射文件>
<! - 将在您指定的sql模式中创建的表 - >
< class> org.drools.persistence.info.SessionInfo< / class>
< class> org.jbpm.persistence.processinstance.ProcessInstanceInfo< / class>
< class> org.drools.persistence.info.WorkItemInfo< / class>
<属性>
< property name =hibernate.default_schemavalue =your_schema_name/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQL5InnoDBDialect/>
< property name =hibernate.connection.autocommitvalue =false/>
< property name =hibernate.max_fetch_depthvalue =3/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.show_sqlvalue =false/>
< property name =hibernate.transaction.manager_lookup_classvalue =org.hibernate.transaction.JBossTransactionManagerLookup/>
< / properties>
< / persistence-unit>
< /持久性>
<3>创建orm.xml
<?xml version =1.0encoding =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>
选择
processInstanceInfo.processInstanceId
从
ProcessInstanceInfo processInstanceInfo
其中
:输入元素(processInstanceInfo.eventTypes)
< / query>
< / named-query>
< / entity-mappings>
4)创建ProcessInstanceInfo.hbm.xml
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd> ;
<! - access =field表示没有setter方法的字段 - >
< class name =ProcessInstanceInfotable =ProcessInstanceInfo>
< id name =processInstanceIdtype =longcolumn =InstanceId>
< generator class =native/>
< / id>
< version name =versiontype =integerunsaved-value =nullaccess =field>
< column name =OPTLOCKnot-null =false/>
< / version>
< property name =processIdaccess =field/>
< property name =startDatetype =timestampaccess =field/>
< property name =lastReadDatetype =timestampaccess =field/>
< property name =lastModificationDatetype =timestampaccess =field/>
< property name =processInstanceByteArraytype =org.hibernate.type.PrimitiveByteArrayBlobType
column =processInstanceByteArrayaccess =fieldlength =2147483647/>
< set name =eventTypestable =EventTypesaccess =field>
< key column =InstanceId/>
< element column =elementtype =string/>
< / set>
<! - 不映射[processInstance]字段,因为字段是暂时的 - >
<! - 不映射[env]字段,因为字段是瞬态的 - >
< / class>
< / hibernate-mapping>
5)现在您必须定义您的数据源。我使用JBoss5,这个版本的JBoss将读取任何带有模式* -ds.xml的文件作为数据源的定义。你必须把这个文件放在你的deploy文件夹中(你可能会注意到那里已经有一个数据源文件,但是不会有冲突)。如果您使用的是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>
<用户名> your_user< /用户名>
<密码> 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文件(谷歌,它太长了)。 7)最后,只需通过EntityManagerFactory调用Java中的持久性单元,创建您的环境并启动一个新的会议。持久性数据应该自动保存到数据库中。
希望这有帮助。干杯!
I'm having some trouble using Persistence on my jBPM project.
My configuration is jBPM 5.4 + Hibernate + JPA 2, and I'm currently setting up the process flow to connect to a DB with persistence, through persistence.xml. I'm just trying to connect the default data source (in the H2 server) with my custom persistence.xml, but I keep getting the same error over and over again:
Unknown entity: org.jbpm.persistence.processinstance.ProcessInstanceInfo
I've manually added to my src/META-INF folder the JBPMorm-JPA2.xml the following content, but the error still persists. Can anyone help me?
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>
UPDATE:
To solve this, create a ProcessInstanceInfo.hbm.xml in the META-INF folder, with the following content:
<?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>
If anyone knows a good tutorial on configuring persistence for jBPM5 please do share...this is insane!
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 helpful https://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!
这篇关于使用JPA 2配置持久性和orm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!