如果你使用的MSSQL数据库就比较不幸了,因为jbpm-4.3\install\src\db\create包里没有提供对于MSSQL的.sql文件,这时需要使用任意一个.sql文件进行转换。比如我这里使用的就是jbpm.mysql.create.sql文件。在powerdesigner里先建库然后在反向映射成model,再根据model建立MSSQL的数据库。建好库后还要插入jbpm-4.3\install\src\demo\example.identities.sql的数据。
接下来首先配置XML文件。直接给出需要的所有配置。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="Spring,com.project"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- datasource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://localhost\mssql2005:1433;databaseName=nhibernate" /> <property name="username" value="sa" /> <property name="password" value="sa2010" /> </bean> <!-- JDBC --> <!-- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> --> <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" /> <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" /> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>HibernatePackage.Model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>jbpm.repository.hbm.xml</value> <value>jbpm.execution.hbm.xml</value> <value>jbpm.history.hbm.xml</value> <value>jbpm.task.hbm.xml</value> <value>jbpm.identity.hbm.xml</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--在程序中使用annotation才会用到--> <!-- <tx:annotation-driven transaction-manager="transactionManager"/> --> <!--这里采用XML配置事务 --> <!-- 以AspectJ方式 定义 AOP 那些类的哪些方法参与事务--> <!-- --> <aop:config> <aop:pointcut id="businessServicePoint" expression="execution(public * Spring.BusinessLogic.Service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="businessServicePoint"></aop:advisor> </aop:config> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="pagedQuery*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean></beans>
以上需要注意的地方是
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>HibernatePackage.Model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>jbpm.repository.hbm.xml</value> <value>jbpm.execution.hbm.xml</value> <value>jbpm.history.hbm.xml</value> <value>jbpm.task.hbm.xml</value> <value>jbpm.identity.hbm.xml</value> </list> </property> </bean>
而不是传统的要额外配置jbpm.hibernate.cfg.xml,这样我们就省了jbpm.hibernate.cfg.xml文件了。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:jbpm.hibernate.cfg.xml</value> </property> </bean>
jbpm.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.tx.spring.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <import resource="jbpm.console.cfg.xml" /> <process-engine-context> <string name="spring.cfg" value="applicationContext.xml" /> </process-engine-context></jbpm-configuration>
log4j.properties
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file hibernate.log ####log4j.appender.file=org.apache.log4j.FileAppender#log4j.appender.file.File=hibernate.log#log4j.appender.file.layout=org.apache.log4j.PatternLayout#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=warn, stdout#log4j.logger.org.hibernate=info#log4j.logger.org.hibernate=debug### log HQL query parser activity#log4j.logger.org.hibernate.hql.ast.AST=debug### log just the SQL#log4j.logger.org.hibernate.SQL=debug### log JDBC bind parameters ####log4j.logger.org.hibernate.type=info#log4j.logger.org.hibernate.type=debug### log schema export/update ###log4j.logger.org.hibernate.tool.hbm2ddl=debug### log HQL parse trees#log4j.logger.org.hibernate.hql=debug### log cache activity ####log4j.logger.org.hibernate.cache=debug### log transaction activity#log4j.logger.org.hibernate.transaction=debug### log JDBC resource acquisition#log4j.logger.org.hibernate.jdbc=debug### enable the following line if you want to track down connection ###### leakages when using DriverManagerConnectionProvider ####log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=tracehandlers= java.util.logging.ConsoleHandlerredirect.commons.logging = enabledjava.util.logging.ConsoleHandler.level = FINESTjava.util.logging.ConsoleHandler.formatter = org.jbpm.internal.log.LogFormatterorg.jbpm.level=FINESTorg.jbpm.pvm.internal.tx.level=FINEorg.jbpm.pvm.internal.wire.level=FINEorg.jbpm.pvm.internal.util.level=FINEorg.hibernate.level=FINEorg.hibernate.cfg.SettingsFactory.level=SEVEREorg.hibernate.cfg.HbmBinder.level=SEVEREorg.hibernate.SQL.level=FINESTorg.hibernate.type.level=FINEST# org.hibernate.tool.hbm2ddl.SchemaExport.level=FINEST# org.hibernate.transaction.level=FINEST
最基本的配置只需要这2个就好了。
web.xml里有了SPRING的配置也基本不需要再动了。
最后再说一下数据转换的问题。
我的数据库用的是MSSQL2005,根据jbpm.mysql.create.sql建好了对应的表并插入了数据。但是现在当程序执行到repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy();
时报了org.hibernate.exception.SQLGrammarException: could not insert: [org.jbpm.pvm.internal.lob.Lob]
说是com.microsoft.sqlserver.jdbc.SQLServerException: 操作数类型冲突: varbinary 与 ntext 不兼容。这时就需要改一下字段属性了。
修改jbpm_lob表里的BLOB_VALUE_,把ntext改为nvarchar(MAX)就OK了。