6
回答
Spring3.1和Hibernate4整合
华为云4核8G,高性能云服务器,免费试用   
只有一个配置文件,只有一个是这样配的,
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

可是我其他用的都是hibernate4

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--
        <property name="annotatedClasses">
            <list>
                <value>com.bjsxt.model.User</value>
                <value>com.bjsxt.model.Log</value>
            </list>
        </property>
         -->
         <property name="packagesToScan">
            <list>
                <value>com.bjsxt.model</value>
                
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <aop:config>
        <aop:pointcut id="bussinessService"
            expression="execution(public * com.bjsxt.service..*.*(..))" />
        <aop:advisor pointcut-ref="bussinessService"
            advice-ref="txAdvice" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="getUser" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED"/>
        </tx:attributes>

    </tx:advice>

启动不报错,运行的时候会报错

java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder
org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:287)
org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233)
org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:393)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)

举报
钟晓文
发帖于6年前 6回/3K+阅
顶部