📄 hibernatejdbcsessionfactory.java
字号:
package org.jbpm.persistence.hibernate;
import java.sql.*;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.mapping.*;
import net.sf.hibernate.tool.hbm2ddl.*;
import org.apache.commons.logging.*;
import org.jbpm.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.model.execution.impl.*;
import org.jbpm.model.log.impl.*;
import org.jbpm.model.scheduler.impl.*;
import org.jbpm.persistence.*;
public class HibernateJdbcSessionFactory implements PersistenceSessionFactory {
protected SessionFactory sessionFactory = null;
public HibernateJdbcSessionFactory(JbpmConfiguration jbpmConfiguration) {
try {
Configuration hibernateConfiguration = createHibernateConfiguration(jbpmConfiguration.getProperties());
sessionFactory = hibernateConfiguration.buildSessionFactory();
initializeTables(hibernateConfiguration, jbpmConfiguration);
initializeIdGenerator(hibernateConfiguration, jbpmConfiguration);
} catch (HibernateException e) {
e.printStackTrace();
throw new RuntimeException( "the hibernate persistence could not be initialized properly : " + e.getMessage() );
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException( "the hibernate persistence could not be initialized properly : " + e.getMessage() );
}
}
public PersistenceSession createPersistenceSession() {
PersistenceSession persistenceSession = null;
try {
persistenceSession = new HibernateSession( sessionFactory.openSession() );
} catch (HibernateException e) {
log.error( "jbpm couldn't create a hibernate session", e );
throw new PersistenceException( "jbpm couldn't create a hibernate session", e );
}
return persistenceSession;
}
public PersistenceSessionTx createPersistenceSessionTx() {
PersistenceSessionTx persistenceSessionTx = null;
try {
persistenceSessionTx = new HibernateSessionJmtTx( sessionFactory.openSession() );
} catch (HibernateException e) {
log.error( "jbpm couldn't create a hibernate session", e );
throw new PersistenceException( "jbpm couldn't create a hibernate session", e );
}
return persistenceSessionTx;
}
private Configuration createHibernateConfiguration(Properties properties) {
// Hibernate configurations
Configuration hibernateConfiguration = new Configuration();
try {
hibernateConfiguration.addClass(ActionImpl.class);
hibernateConfiguration.addClass(DefinitionImpl.class);
hibernateConfiguration.addClass(NodeImpl.class);
hibernateConfiguration.addClass(TransitionImpl.class);
hibernateConfiguration.addClass(SwimlaneImpl.class);
hibernateConfiguration.addClass(TypeImpl.class);
hibernateConfiguration.addClass(VariableImpl.class);
hibernateConfiguration.addClass(DelegationImpl.class);
hibernateConfiguration.addClass(FileImpl.class);
hibernateConfiguration.addClass(ProcessInstanceImpl.class);
hibernateConfiguration.addClass(TokenImpl.class);
hibernateConfiguration.addClass(VariableInstanceImpl.class);
hibernateConfiguration.addClass(InvocationLogImpl.class);
hibernateConfiguration.addClass(ExecutionLogImpl.class);
hibernateConfiguration.addClass(JobImpl.class);
hibernateConfiguration.addProperties(properties);
} catch (MappingException e) {
e.printStackTrace();
throw new PersistenceException("couldn't create a hibernate configuration because of a mapping problem", e);
}
return hibernateConfiguration;
}
private void initializeTables(Configuration hibernateConfiguration, JbpmConfiguration jbpmConfiguration) throws HibernateException, SQLException {
Session session = null;
try {
session = sessionFactory.openSession();
boolean createTablesIfNotPresent = jbpmConfiguration.getBoolean("jbpm.create.tables");
boolean logDdlToStdOut = jbpmConfiguration.getBoolean("jbpm.create.tables.log");
// if the table existance need to be checked
if ( createTablesIfNotPresent ) {
// check if the table JBPM_DEFINITION already exist.
ResultSet rs = session.connection().getMetaData().getTables(null, null, "JBPM_DEFINITION", null);
if (!rs.next()) {
// it seems not, creating the tables...
log.info("no JBPM_ tables in the database, creating the tables for you...");
try {
new SchemaExport(hibernateConfiguration).create(logDdlToStdOut, true);
log.debug("created the JBPM_ database tables successfully");
} catch (RuntimeException e) {
throw new PersistenceException("couldn't create the -not-existing- database tables : " + e.getMessage());
}
}
}
} finally {
if ( session != null ) session.close();
}
}
private void initializeIdGenerator(Configuration hibernateConfiguration, JbpmConfiguration jbpmConfiguration) throws HibernateException {
Session session = null;
try {
session = sessionFactory.openSession();
// get the configured id-generator
IdGenerator idGenerator = (IdGenerator) jbpmConfiguration.instantiate("jbpm.id.generator", IdGenerator.class);
// if the default id-generator is used,
if ( idGenerator instanceof NonClusterableIdGenerator ) {
long highestId = 1;
try {
// loop over all jbpm tables
Iterator iter = hibernateConfiguration.getClassMappings();
while (iter.hasNext()) {
PersistentClass persistentClass = (PersistentClass) iter.next();
// get the highest id that is used
Long maxId = (Long) session.find("select max( t.id ) from t in class " + persistentClass.getName()).get(0);
if ((maxId != null) && (maxId.longValue() > highestId)) {
highestId = maxId.longValue();
}
}
// add one
highestId++;
// provide that value to initialize the default id generator
NonClusterableIdGenerator.initialize( highestId );
} catch (HibernateException e) {
e.printStackTrace();
throw new PersistenceException( "jbpm couldn't scan for the highest used id", e);
}
}
ConfigurableIdGenerator.setIdGenerator(idGenerator);
} finally {
if ( session != null ) session.close();
}
}
SessionFactory getSessionFactory() {
return sessionFactory;
}
private static Log log = LogFactory.getLog(HibernateJdbcSessionFactory.class);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -