SpringMVC+SpringSecurity+Hibernate 整合问题

链接参考  http://hanqunfeng.iteye.com/blog/2114987


一.  表单提交错误

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.


 @requestBody 是针对json提交的, 对于表单提交不用注解也能自动注入到参数实体里面



二. hibernate4 和hibernate5 配置混乱是的错误


Could not obtain transaction-synchronized Session for current thread
加上@Transactional




java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext


两种可能
1.hibernate是5.0版本,而spring是4.0版本。5.0版本的hibernate中的相应包中把那个类给取消了。而在spring中配置时,我们最多只能配置到hibernate4,所以就出现了上述问题。
解决很简单,去网上下载hibernate4.0版本的hibernate-core-4.3.8.Final.jar,用这个文件替换中5.0中的那个。现在再运行,就能正常工作了。
2.spring-hibernate.xml中,hibernate指定包名或Java文件名写错,导致找不到该文件。
或者直接复制一个hibernate4的配置文件,忘记将其中的hibernate4改为hibernate5,导致找不到相应文件。


我的修改方案: 直接使用HibernateTransactionManager和LocalSessionFactoryBean的5版本
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;



getcurrentSession 报错说明
https://wenku.baidu.com/view/912d9a370b4c2e3f57276336.html
hibernate4不支持你用hibernate3的 getcurrentSession,建议你用openSession



spring hibernate 5 Error Already value [org.springframework.orm.hibernate5.SessionHolder for key bound to thread
或者   No value for key [org.hibernate.internal.SessionFactoryImpl@1f0a19b7] bound to thread [http-apr-8080-exec-10]


方案1.
I Found a solution for this problem. I was using hibernate 4 sf bean .
org.springframework.orm.hibernate4.LocalSessionFactoryBean
I had to change it to hibernate 5 sf bean to fix the problem
org.springframework.orm.hibernate5.LocalSessionFactoryBean


方案2
    <!-- 指定了Hibernate的映射文件和配置信息 -->    
        <bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">    
            <property name="dataSource">    
                <ref local="dataSource" />    
            </property>    
            <property name="hibernateProperties">    
                <props>    
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>    
                    <prop key="show_sql">true</prop>    
                    <prop key="hibernate.jdbc.batch_size">20</prop>    
                    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>  
                </props>    
            </property>    
            <property name="packagesToScan" value="*" />            
        </bean>    
         <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">    
            <property name="sessionFactory" ref="sessionFactory" />    
        </bean>   


<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
给予javaconfig 的需要配置LocalSessionFactoryBean的hibernateProperties
高亮显示的部分是配置hibernate4和hibernate5时必须要加上的,hibernate3之前的版本不需要这一句。


spring和hibernate版本兼容问题


目前Spring3.2.9只能支持Hibernate4.2,不能支持更高版本的,所以导入jar包时需要注意。




五 spring 上下文
https://my.oschina.net/jast90/blog/282773


六 数据库连接错误
连接MySQL数据库时报以下时区错误信息:
java.sql.SQLException: The server time zone value  is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.


解决方案:
 
为URL添加参数serverTimezone=UTC即可,这里的时区可以根据自己数据库的设定来设置(GMT/UTC )。
jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC




@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />


事务管理器
// 创建事务管理器1
    @Bean(name = "txManager1")
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    // 创建事务管理器2
    @Bean(name = "txManager2")
    public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
        return new JpaTransactionManager(factory);
    }



getcurrentSession 和 opensession 的区别


Hibernate openSession() 和 getCurrentSession的区别


 


getHiberanteTemplate 、getCurrentSession和OpenSession 
采用getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。


采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭。


采用getCurrentSession()需要在Hibernate.cfg.xml配置文件中加入如下配置:


如果是本地事物,及JDBC一个数据库:


<propety name=”Hibernate.current_session_context_class”>thread</propety>


如果是全局事物,及jta事物、多个数据库资源或事物资源:


<propety name=”Hibernate.current_session_context_class”>jta</propety>


使用spring的getHiberanteTemplate 就不需要考虑事务管理和session关闭的问题:


 


openSession创建session时autoCloseSessionEnabled参数为false,即在事物结束后不会自动关闭session,需要手动关闭,如果不关闭将导致session关联的数据库连接无法释放,最后资源耗尽而使程序当掉。              


getCurrentSession创建session时autoCloseSessionEnabled,flushBeforeCompletionEnabled都为true,并且session会同sessionFactory组成一个map以sessionFactory为主键绑定到当前线程。


getCurrentSession():从上下文(配置文件current_session_context_class: thread 使用Connection自动管理;jta(java transaction api) 由Application Server提供的分布式事务管理,Tomcat本身不具备此能力,JBoss、WebLogic具备)找,如果有,则用旧的,否则创建新的,事务提交自动Close;


getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置:


如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值