hibernate session 到SpringMVC Controller 层后 为null?

问题描述

basedaoimpl:@AutowiredprivateSessionFactorysessionFactory;publicSessionFactorygetSessionFactory(){returnsessionFactory;}publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicSessiongetSession(){returnsessionFactory.getCurrentSession();}publicSerializablesave(Objectobj){try{returnsessionFactory.getCurrentSession().save(obj);}catch(Exceptione){e.printStackTrace();}return0;}publicvoidupdate(Objectobj){try{sessionFactory.getCurrentSession().update(obj);}catch(Exceptione){e.printStackTrace();}}publicvoidsaveOrUpdate(Objectobj){try{sessionFactory.getCurrentSession().saveOrUpdate(obj);}catch(Exceptione){e.printStackTrace();}}publicvoiddelete(Objectobj){try{sessionFactory.getCurrentSession().delete(obj);}catch(Exceptione){e.printStackTrace();}}publicObjectget(Class<?>cls,intid){try{returnsessionFactory.getCurrentSession().get(cls,id);}catch(Exceptione){e.printStackTrace();}returnnull;}publicObjectload(Class<?>cls,intid){try{returnsessionFactory.getCurrentSession().load(cls,id);}catch(Exceptione){e.printStackTrace();}returnnull;}publicList<?>list(Class<?>cls){try{returnsessionFactory.getCurrentSession().createCriteria(cls).list();}catch(Exceptione){e.printStackTrace();}returnnull;}projectdaoimplpublicintsave(Projectproject){return(Integer)super.save(project);}publicvoidupdate(Projectproject){super.update(project);}publicvoidsaveOrUpdate(Projectproject){super.saveOrUpdate(project);}publicvoiddelete(Projectproject){super.delete(project);}publicProjectget(intid){return(Project)super.get(Project.class,id);}publicProjectload(intid){return(Project)super.load(Project.class,id);}@SuppressWarnings("unchecked")publicList<Project>list(){return(List<Project>)super.list(Project.class);}projectbizimpl:@AutowiredprivateProjectDaoprojectDao;publicProjectDaogetProjectDao(){returnprojectDao;}publicvoidsetProjectDao(ProjectDaoprojectDao){this.projectDao=projectDao;}publicintsaveProject(Projectproject){returnprojectDao.save(project);}publicvoidupdateProject(Projectproject){projectDao.update(project);}publicvoidsaveOrUpdateProject(Projectproject){projectDao.saveOrUpdate(project);}publicvoiddeleteProject(intid){projectDao.delete(getProject(id));}publicProjectgetProject(intid){returnprojectDao.get(id);}publicList<Project>listProject(){returnprojectDao.list();}projectcontrollerimpl:@RequestMapping("project/input")publicModelAndViewinputProject(intid){ModelAndViewmodelAndView=newModelAndView("/project/update");Projectproject=projectBiz.getProject(id);//报错couldnotinitializeproxy-noSession//debug后发现set集合里没有session,而biz层有project.getProjectForms().iterator();project.getProjectAttachments().iterator();modelAndView.addObject("project",project);modelAndView.addObject("departments",departmentBiz.listDepartment());returnmodelAndView;}pojo:@Id@GeneratedValue(strategy=GenerationType.AUTO)privateintid;privateStringname;@ManyToOne(targetEntity=Department.class)privateDepartmentdepartment;privatedoubleamount;privateStringsource;privateStringdescription;privatebooleancomplete;privateDatecreateDate;@OneToMany(targetEntity=ProjectForm.class,cascade=CascadeType.ALL)@JoinColumn(name="project_id")@OrderBy("noasc")privateSet<ProjectForm>projectForms=newHashSet<ProjectForm>();@OneToMany(targetEntity=ProjectAttachment.class,cascade=CascadeType.ALL)@JoinColumn(name="project_id")@OrderBy("noasc")privateSet<ProjectAttachment>projectAttachments=newHashSet<ProjectAttachment>();applicationContext<!--解析视图--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/"/><propertyname="suffix"value=".jsp"/></bean><!--静态资源过滤--><mvc:resourceslocation="/img/"mapping="/img/**"/><mvc:resourceslocation="/css/"mapping="/css/**"/><mvc:resourceslocation="/js/"mapping="/js/**"/><mvc:resourceslocation="/upload/"mapping="/upload/**"/><!--扫描注解组件--><context:component-scanbase-package="com.cepd"/><!--自动注册HandlerMapping、HandlerAdapter、ExceptionResolver等MVC的相关实例--><mvc:annotation-driven/><!--数据源--><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost/cepd?characterEncoding=utf-8"/><propertyname="username"value="root"/><propertyname="password"value="rootme"/></bean><!--集成hibernate4--><beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!--注入数据源--><propertyname="dataSource"ref="dataSource"/><!--设置参数--><propertyname="hibernateProperties"><props><propkey="hibernate.show_sql">true</prop><propkey="hibernate.hbm2ddl.auto">update</prop><propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><!--扫描注解实体类--><propertyname="packagesToScan"value="com.cepd.pojo"/></bean><!--事务管理--><beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--采用注解方式处理事务--><tx:annotation-driventransaction-manager="transactionManager"/><!--文件上传--><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--最大上传大小1000kb--><propertyname="maxUploadSize"value="1000000"/></bean>web<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>openSessionInView</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSessionInView</filter-name><url-pattern>/*</url-pattern></filter-mapping>

请问session可以传到web层吗

解决方案

本帖最后由 kipopo30 于 2013-11-14 15:54:56 编辑
解决方案二:
没有加事务
解决方案三:
表现层->业务逻辑层->hibernate->数据库直接由hibernate到表现层我觉得设计不合理。

时间: 2024-12-14 04:46:34

hibernate session 到SpringMVC Controller 层后 为null?的相关文章

【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目

研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过程中介绍. 大概就是下面的几个步骤: 1. 设计数据库,得到数据库 2.使用maven搭建项目,导入包的依赖 3.引入spring的配置信息,放在资源文件中 4.引入Log4j日志文件配置 5.配置hibernate 6.配置ehcache.xml 7.使用hibernate  tools生成实体

spring-求解决No Hibernate Session bound to thread

问题描述 求解决No Hibernate Session bound to thread ssh项目在运行时控制台没有出错,但是页面出现 HTTP Status 500 - No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here怎么解决??? applicationContext.xml文件配置如下: <?xml version=&quo

Hibernage错误:Could not open Hibernate Session for transaction

今天在做SSH框架整合的时候碰到的这个问题,在测试service层的时候程序正常运行,但是添加Struts,在action层测试的时候提示了如下问题:Could not open Hibernate Session for transaction.大概意思就是数据库连接超时. 解决方法如下: 在spring的配置文件中添加如下配置 给sessionFactory的bean添加如下配置 <bean id="sessionFactory" class="org.spring

session-【资深的你,进来指点下】SpringMvc Controller的线程安全!

问题描述 [资深的你,进来指点下]SpringMvc Controller的线程安全! SpringMvc Controller的**线程安全**! 为什么下面的代码是线程安全的? @Controllerpublic class UserController{ @Autowired private HttpSession session @RequestMapping(xxxxxxx){ session.get ... session.set... .... } }大家都知道spring bea

关于spring controller层的设计

问题描述 最近在做一个关于相册网站的项目,由于本人经验严重欠缺,需要各位大侠帮助一下.项目是基于Springmvc+ibatis+velocity的.项目大概划分为Dao层.Servic层.Domain层.Controller层等几个大层.比如Service层里有PhotoDao,AlbumDao等.Service层里面有PhotoService.AlbumService等.把各个Dao注入到相应Service层里.各个Controller里注入相应Service.比如说我要上传一张图片,将图片

简化Spring(3) Controller层

Struts与Webwork的扇子请跳过本篇. MVC不就是把M.V.C分开么?至唯物朴素的做法是两个JSP一个负责View,一个负责Controller,再加一个负责Model的Java Bean,已经可以工作得很好,那时候一切都很简单. 而现在为了一些不是本质的功能,冒出这么多非标准的Web框架,实在让人一阵郁闷.像Ruby On Rails那样简捷开发,可用可不用,而且没有太多的限制需要学习的,比如Webwork这型还可以考虑.但像Struts那样越用框架越麻烦,或者像Tapestry那样

session-Could not open Hibernate Session for transaction

问题描述 Could not open Hibernate Session for transaction ssh集成访问action时候报这个错误,数据库是oracle11g,数据库连接池用的c3p0,按照搜到的配置自动重连也没用.使用PLSQL可以连接到oracle spring配置文件关于数据库连接池和sessionFactory的内容如下: <bean id="sessionFactory" class="org.springframework.orm.hibe

spring-mvc controller能访问到但无法跳转到静态jsp页面

问题描述 spring-mvc controller能访问到但无法跳转到静态jsp页面 问题很小,但是一直没找出问题来,maven项目build没问题,运行也没问题,就是访问的时候能访问到controller但无法跳转到jsp页面,路径问题还是xml配置问题呢. 求大神指点... 这是目录结构 这是web.xml配置和spring-mvc配置 这个是controller 解决方案 将jsp文件夹放到WEB-INF下面,然后配置里面 /WEB-INF/jsp/ 这样写 解决方案二: 你把url-p

2013/11/22工作随笔-缓存是放在Model层还是放在Controller层

web网站的典型代码框架就是MVC架构,Model层负责数据获取,Controller层负责逻辑控制,View层则负责展示. 一般数据获取是去mysql中获取数据 但是这里有个问题,我们不会每次请求都去mysql中获取,当并发量大的时候,我们希望mysql前面有层缓存做着. 这层缓存可能是redis,memcache,File等,但是这个逻辑代码是放在Controller中好还是Model中好呢? 放在Controller中: 好处:可以单个Controller进行控制,每个controller