spring4+springmvc+springdataJPA+hibernate4+Junit4整合懒加载问题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cyzshenzhen/article/details/45420513

文章目录


#摘要
本文主要是为了解决“spring4+springmvc+springdataJPA+hibernate4+junit4整合”,注解了OneToMany、ManyToOne,并配置了懒加载,然后报错的问题

##第一个问题

org.hibernate.LazyInitializationException: could not initialize proxy - no Session at

###解决办法:
在web.xml中,配置springSevlet的位置,使用org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter,代码如下:

<filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

##第二个问题

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ......., no session or session was closed

一般是在定时任务quartzJob中,或者Junit测试中使用懒加载出现的错误

###解决办法:
添加事务配置注解。junit代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"file:src/main/webapp/WEB-INF/applicationContext.xml",
		"file:src/main/webapp/WEB-INF/demo-servlet.xml"})
@Transactional
public class Test {

完毕。

— --- — ---> 点击查看更多最新原创博文<— --- — ---

技术交流

在这里插入图片描述

展开阅读全文

struts2+ hibernate4 +spring4问题求助

08-12

rnrnException in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/service/jndi/JndiNameExceptionrn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)rn at com.bufoon.util.Util.main(Util.java:68)rnCaused by: java.lang.NoClassDefFoundError: org/hibernate/service/jndi/JndiNameExceptionrnrnrnapplicationContext.xmlrnrnrn rn rn rn rn rn rn rn rn com.bufoon.entityrn rn rn 论坛

没有更多推荐了,返回首页