Hi. Sorry for repeating the title of the thread, but all advices in other threads didn't help me. I'm making Spring MVC 3.1.1 + Hibernate app 3.5, netbeans 7.2.
my entity class News.java:
it's my DAO interface NewsDAO.java:Code:package by.Entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * * @author Serge */ @Entity @Table(name="News") public class News { private Long id; private String newsText; @Id @GeneratedValue @Column(name="ID") public Long getId(){ return id; } public void setId(Long id){ this.id = id; } @Column(name="NEWS_TEXT") public String getNewsId(){ return newsText; } public void setNewsText(String newsText){ this.newsText = newsText; } }
Interface implementation:Code:package by.DAO; import by.Entity.News; import java.util.List; /** * * @author Serge */ public interface NewsDAO{ public void saveNews(News news); public void readNews(Long id); public void deleteNews(Long id); public List<News> listNews(); }
Here is the service flow. NewsService.java:Code:package by.DAO; import by.Entity.News; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author Serge */ @Repository public class NewsDAOImpl implements NewsDAO{ // @Autowired private SessionFactory sessionFactory; @Override public void saveNews(News news) { sessionFactory.getCurrentSession().save(news); } @Override public void readNews(Long id) { sessionFactory.getCurrentSession().load(News.class, id); } @Override public void deleteNews(Long id) { News news = (News) sessionFactory.getCurrentSession().load(News.class, id); if (null != news){ sessionFactory.getCurrentSession().delete(news); } } @Override public List<News> listNews() { return sessionFactory.getCurrentSession().createQuery("from News") .list(); } }
NewsServiceImpl.java:Code:package by.service; import by.Entity.News; import java.util.List; /** * * @author Serge */ public interface NewsService { public void saveNews(News news); public void readNews(Long id); public void deleteNews(Long id); public List<News> listNews(); }
My controller:Code:package by.service; import by.DAO.NewsDAO; import by.Entity.News; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * * @author Serge */ @Service public class NewsServiceImpl implements NewsService{ @Autowired private NewsDAO newsDAO; @Transactional @Override public void saveNews(News news) { newsDAO.saveNews(news); } @Transactional @Override public void readNews(Long id) { newsDAO.readNews(id); } @Transactional @Override public void deleteNews(Long id) { newsDAO.deleteNews(id); } @Transactional @Override public List<News> listNews() { return newsDAO.listNews(); } }
Next are my xml files. I use web.xml obviously and dispatcher-servlet.xml only. ApplicationContext.xml is empty.Code:package by.controllers; /** * * @author Serge */ import by.Entity.News; import by.service.NewsService; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PagesController { @Autowired private NewsService newsService; @RequestMapping("/main") public String goHome(Map<String, Object> map){ map.put("news", new News()); map.put("newsList", newsService.listNews()); return "main"; } @RequestMapping("/news") public String goToNews(){ return "news"; } @RequestMapping("/games") public String goToGames(){ return "games"; } @RequestMapping("/video") public String goToVideo(){ return "video"; } @RequestMapping("/forum") public String goToForum(){ return "forum"; } }
web.xml:
i got this error:Code:<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <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/dispatcher-servlet.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> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> <!-- TransactionFilterException --> <!-- <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>--> </web-app>
i have no idea, because a have sessionFactory in my dispatcher-servlet.xml.Code:type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097) org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:242) org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:227) org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs
I haven't read the spring transaction docs yet but read several tutorials how to make spring mvc app and did exactly the same stuff. Please help, thnx.


Reply With Quote