问题描述
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到表现层我觉得设计不合理。