我在JBPM 6.4中创建了一个新的存储库和一个新项目。在项目内部,我创建了一个persistence.xml
:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence 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" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<persistence-unit name="demox:com.prueba:1.0" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/jbpmDS</jta-data-source>
<class>demox.com.prueba.Estudiante</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.id.new_generator_mappings" value="false"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
我还定义了一个可持续的对象(用@Entity
装饰的pojo。)
现在我有一个只有一个脚本任务的简单流程。
但是当我在我的数据库中部署流程时,Student
表已创建...
package demox.com.prueba;
/**
* This class was automatically generated by the data modeler tool.
*/
@javax.persistence.Entity
public class Estudiante implements java.io.Serializable
{
static final long serialVersionUID = 1L;
@javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO, generator = "ESTUDIANTE_ID_GENERATOR")
@javax.persistence.Id
@javax.persistence.SequenceGenerator(name = "ESTUDIANTE_ID_GENERATOR", sequenceName = "ESTUDIANTE_ID_SEQ")
private java.lang.Long id;
@org.kie.api.definition.type.Label(value = "name")
private java.lang.String name;
public Estudiante()
{
}
public java.lang.Long getId()
{
return this.id;
}
public void setId(java.lang.Long id)
{
this.id = id;
}
public java.lang.String getName()
{
return this.name;
}
public void setName(java.lang.String name)
{
this.name = name;
}
public Estudiante(java.lang.Long id, java.lang.String name)
{
this.id = id;
this.name = name;
}
}
所以问题是:如何进入此scriptTask
对此EntityManger
的引用以保持此pojo?
我试过了:
demox.com.prueba.Estudiante est=new demox.com.prueba.Estudiante();
String pu="demox:com.prueba:1.0";
//javax.persistence.EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory( pu );
javax.persistence.EntityManagerFactory emf = (javax.persistence.EntityManagerFactory) kcontext.getKnowledgeRuntime().getEnvironment()
.get(org.kie.api.runtime.EnvironmentName.ENTITY_MANAGER_FACTORY); # tried also EnvironmentName.CMD_SCOPED_ENTITY_MANAGER
System.out.println("Entity manager " + emf );
emf.persist(est);
但是当使用这段代码时,EntityManager是对JBPM持久性单元的引用,而不是我的持久性单元(demo)的实例,它不包含我的pojo类(未知实体)。
那么如何在我的脚本任务中访问这个持久性单元呢?
答案 0 :(得分:0)
建议不要在script tasks中执行数据库调用等复杂操作。您应该使用service task代替您自己的work item handler实施。您已经可以使用自定义JPAWorkItemHandler实现。