ssh整合问题

我前面整合的spring3+struts2+hibernate4.由于要使用jbpm4,所以hibernate要换成3.
这是spring配置文件
<!-- 开启注解处理器 -->
<context:annotation-config />
<!-- 扫描包路径 -->
<context:component-scan base-package="com.eport" />
<!-- 加载jdbc.properties文件   -->
    <context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源   -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${driverClassName}" />
   <property name="jdbcUrl" value="${jdbcUrl}" /> 
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="10" />
<property name="initialPoolSize" value="1" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryAttempts" value="3" />
<property name="maxIdleTime" value="3600" />
</bean>

<!-- 整合hibernate --> 
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<list>
<value>classpath:jbpm.execution.hbm.xml</value>
<value>classpath:jbpm.history.hbm.xml</value>
<value>classpath:jbpm.identity.hbm.xml</value>
<value>classpath:jbpm.repository.hbm.xml</value>
<value>classpath:jbpm.task.hbm.xml</value>
    </list>
</property>
</bean>

<!-- 配置jBPM的流程引擎 -->
<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">
<property name="jbpmCfg" value="jbpm.cfg.xml" />
</bean>
<bean id="processEngine" factory-bean="springHelper"  factory-method="createProcessEngine" />

<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>


<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
这是hibernate配置文件
<hibernate-configuration> 
    <session-factory > 
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> 
        <property name="show_sql">false</property> 
        <property name="format_sql">true</property> 
        <property name="hbm2ddl.auto">update</property> 
 
        <property name="current_session_context_class">
        org.springframework.orm.hibernate3.SpringSessionContext
        </property>
 
  <mapping class="com.eport.account.pojo.Account"/>
  <mapping class="com.eport.account.pojo.PrivilegeGroup"/>
  <mapping class="com.eport.account.pojo.SystemPrivilege"/>
    </session-factory> 
</hibernate-configuration> 
这是dao配置文件
@Transactional
public class DaoSupport<T> implements Dao<T>{
@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;
private final Class<T> entityClass;

@SuppressWarnings("unchecked")
public DaoSupport(){
this.entityClass = (Class<T>) ((ParameterizedType) getClass().
getGenericSuperclass()).getActualTypeArguments()[0]; 
}

public Session getSession(){
return sessionFactory.getCurrentSession();
}

@Override
public long getCount() {
Query query = this.getSession().createQuery("select count(" +
DaoSupport.getCountField(this.entityClass) + ") from " +
this.buildEntityName()+" o");
return new Long(query.uniqueResult().toString()).longValue();
}

@Override
public void delete(T entity) {
this.getSession().delete(entity);
}

@Override
public void save(T entity) {
this.getSession().save(entity);
}

@Override
public void update(T entity) {
this.getSession().update(entity);
}

@SuppressWarnings("unchecked")
@Override
@Transactional(propagation=Propagation.REQUIRED)
public T find(Serializable id) {
return (T) getSession().get(this.entityClass, id); 
}

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int startIndex, int maxresult,
String jpql, Object[] params) {
return this.getScrollData(startIndex, maxresult, jpql, params, null);
}

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int startIndex, int maxresult,
LinkedHashMap<String, String> orderby) {
return this.getScrollData(startIndex, maxresult, null, null, orderby);
}

public QueryResult<T> getScrollData(String jpql, Object[] params,
LinkedHashMap<String, String> orderby) {
return this.getScrollData(-1, -1, jpql, params, orderby);
}

@SuppressWarnings( { "unchecked", "static-access" })
public QueryResult<T> getScrollData(int startIndex, int maxresult,
String jpql, Object[] params, LinkedHashMap<String, String> orderby) {
QueryResult<T> qr = new QueryResult<T>();
String entityName = buildEntityName();

String whereJpql = "";
if (jpql != null && !"".equals(jpql)) {
whereJpql = "where " + jpql;
}
Query query = this.getSession().createQuery("select o from " + entityName +
" o " + whereJpql + methodOrderby(orderby));
if (jpql != null && !"".equals(jpql)) {
this.setParms(query, params);
}
if (startIndex != -1 && maxresult != -1) {// 只有这两个参数都不等于-1,才进行分页
query.setFirstResult(startIndex);
query.setMaxResults(maxresult);
}

qr.setResultList(query.list());

// 统计查询记录数
query = this.getSession().createQuery("select count(" +
this.getCountField(this.entityClass) + ") from " + entityName + " o "
+ whereJpql);
if (jpql != null && !"".equals(jpql)) {
this.setParms(query, params);
}
qr.setTotalrecords(new Long(query.uniqueResult().toString()).longValue());
return qr;
}

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData() {
return this.getScrollData(-1, -1, null, null, null);
}

@Override
public void flush() {
this.getSession().flush();
}

@Override   
public void clear() {
this.getSession().clear();
}

private String methodOrderby(LinkedHashMap<String, String> orderby) {
StringBuilder sb = new StringBuilder();
if (orderby != null && orderby.size() > 0) {
sb.append(" order by ");
for (String key : orderby.keySet()) {
sb.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
}
sb.deleteCharAt(sb.length() - 1);// 删除最后多余的逗号
}
return sb.toString();
}

/**
* 对query对象进行参数赋值
*
* @param query
* @param params
*/
private void setParms(Query query, Object[] params) {
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i , params[i]);
}
}
}


/**
* 通过实体clazz对象,构建查询的实体名字
*
* @return
*/
private String buildEntityName() {
String name = entityClass.getSimpleName();
Entity entity = entityClass.getAnnotation(Entity.class);
if (entity.name() != null && !"".equals(entity.name().trim())) {
name = entity.name();
}
return name;
}

protected static <E> String getCountField(Class<E> clazz) {
String out = "o";
try {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
for (PropertyDescriptor propertydesc : propertyDescriptors) {
Method method = propertydesc.getReadMethod();
if (method != null && method.isAnnotationPresent(EmbeddedId.class)) {
PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType())
.getPropertyDescriptors();
out = "o." + propertydesc.getName() + "."
+ (!ps[1].getName().equals("class") ? ps[1].getName() : ps[0].getName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return out;
}


}
但是我在运行的时候报了错误
java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder
hibernate4.SessionHolder不能转换为hibernate3.SessionHolder。但是我都改成hibernate3
了啊。不知道这个hibernate4是哪里来的

猜你喜欢

转载自201210271615.iteye.com/blog/1884575
今日推荐