Warning : Not mapping found with URI in dispatcher servlet - spring

Controller is :
package com.project.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.project.model.User;
import com.project.service.UserService;
#RestController
#RequestMapping("/testmap")
public class TestController {
#Autowired
private UserService userService;
#RequestMapping(value="/test", method = RequestMethod.GET, produces = "application/json")
public #ResponseBody List<User> testMethod(){
try{
System.out.println("--------------inside testmethod----------------------------");
return userService.getAllUser();
}catch(Exception e){
System.out.println("Exception Occur = "+e);
return null;
}
}
}
Service is look like :
package com.project.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.project.dao.UserDAO;
import com.project.model.User;
#Service
#Configuration
#EnableWebMvc
public class UserService {
#Autowired
private UserDAO userDAO;
#Transactional(readOnly = true)
public List<User> getAllUser() {
return userDAO.fetchAll(User.class);
}
}
DAO is look like :
package com.project.dao;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.project.common.HibernateUtil;
#Repository
#Configuration
#EnableWebMvc
public class UserDAO extends HibernateUtil {
}
Generic DAO is :
package com.project.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#Repository
#Configuration
#EnableWebMvc
public class HibernateUtil {
#Autowired
private SessionFactory sessionFactory;
public <T> Serializable create(final T entity) {
return sessionFactory.getCurrentSession().save(entity);
}
public <T> T update(final T entity) {
sessionFactory.getCurrentSession().update(entity);
return entity;
}
public <T> void delete(final T entity) {
sessionFactory.getCurrentSession().delete(entity);
}
public <T> void delete(Serializable id, Class<T> entityClass) {
T entity = fetchById(id, entityClass);
delete(entity);
}
public <T> void saveOrUpdate(final T o){
sessionFactory.getCurrentSession().saveOrUpdate(o);
}
public <T> List<T> getAll(final Class<T> type) {
final Criteria crit = sessionFactory.getCurrentSession().createCriteria(type);
return crit.list();
}
#SuppressWarnings("unchecked")
public <T> List<T> fetchAll(Class<T> entityClass) {
return sessionFactory.getCurrentSession().createQuery(" FROM "+entityClass.getName()).list();
}
#SuppressWarnings("rawtypes")
public <T> List fetchAll(String query) {
return sessionFactory.getCurrentSession().createSQLQuery(query).list();
}
#SuppressWarnings("unchecked")
public <T> T fetchById(Serializable id, Class<T> entityClass) {
return (T)sessionFactory.getCurrentSession().get(entityClass, id);
}
}
web.xml is :
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
rest-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Specifying base package of the Components like Controller, Service,
DAO -->
<context:component-scan
base-package="com.project" />
<!-- Getting Database properties -->
<context:property-placeholder location="classpath:application.properties" />
<mvc:annotation-driven />
<!-- Specifying the Resource location to load JS, CSS, Images etc -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- View Resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driverClass}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="acquireIncrement" value="${connection.acquireIncrement}" />
<property name="minPoolSize" value="${connection.minPoolSize}" />
<property name="maxPoolSize" value="${connection.maxPoolSize}" />
<property name="maxIdleTime" value="${connection.maxIdleTime}" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.project.model"></property>
</bean>
<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
my project configuration is above. Actually the problem is that no mapping found for 'http://localhost:8080/ShoppingProject/testmap/test' and warning is below
2017-06-24 13:19:39 DEBUG DispatcherServlet:865 - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/ShoppingProject/testmap/test]
2017-06-24 13:19:39 DEBUG RequestMappingHandlerMapping:310 - Looking up handler method for path /testmap/test
2017-06-24 13:19:39 DEBUG RequestMappingHandlerMapping:320 - Did not find handler method for [/testmap/test]
2017-06-24 13:19:39 WARN PageNotFound:1147 - No mapping found for HTTP request with URI [/ShoppingProject/testmap/test] in DispatcherServlet with name 'mvc-dispatcher'
2017-06-24 13:19:39 DEBUG DispatcherServlet:1000 - Successfully completed request
I am stuck with what is problem in mapping? Any help will be greatly appreciated

Related

Unable to insert record into DB using hibernate integrated with Spring using XML

I am trying to implement a signup page using Spring MVC integrated with hibernate using XML.
Application Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<property name="template" ref="template"></property>
</bean>
</beans>
Controller Class
package com.uday;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class ControllerSignUp_Login {
public ControllerSignUp_Login() {
// TODO Auto-generated constructor stub
}
#RequestMapping("/hello")
public String diaplay(#RequestParam("name") String name, #RequestParam("pass") String pass,Model m) {
Login_DetailsDao dao = (Login_DetailsDao) getBean();
if(dao.isLogoinSuccessfull(name , pass)) {
m.addAttribute("message", "Hello"+name);
return "Success";
}
else {
m.addAttribute("message", "You have Entered Wrong pin");
return "Failure";
}
}
#RequestMapping("/SignUp")
public String redirect() {
System.out.println("ControllerSignUp_Login.display()");
return "signup";
}
#RequestMapping("/login")
public String display() {
System.out.println("ControllerSignUp_Login.display()");
return "login";
}
#RequestMapping("/updateDetails")
#Transactional
public String display(HttpServletRequest req , Model M) {
String firstName=req.getParameter("firstName");
String lastName=req.getParameter("lastName");
String mobileNo=req.getParameter("mobileNo");
String address=req.getParameter("address");
String city=req.getParameter("city");
String password=req.getParameter("password");
Login_DetailsDao dao = (Login_DetailsDao) getBean();
if(checkLength(firstName) && checkLength(lastName) && checkLength(mobileNo) && checkLength(address) && checkLength(city) && checkLength(password)) {
Login_Details ld = new Login_Details();
ld.setFirstName(firstName);
ld.setLastName(lastName);
ld.setCity(city);
ld.setAddress(address);
ld.setMobileNo(mobileNo);
ld.setPassword(password);
dao.saveEmployee(ld);
M.addAttribute("message", "SignUp Successfull !! Thank You");
M.addAttribute("displayLogin", true);
return "Success";
}
else {
M.addAttribute("message","SignUp Failed !! All details are mandatory.");
return "signup";
}
}
public boolean checkLength(String s) {
if(s != null && s.length() > 0) {
return true;
}
return false;
}
public Object getBean() {
ApplicationContext appcontext = new ClassPathXmlApplicationContext("Applicationcontext.xml");
Login_DetailsDao lDDao =(Login_DetailsDao)appcontext.getBean("d");
return lDDao;
}
}
DAO class
package com.uday;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.transaction.annotation.Transactional;
public class Login_DetailsDao {
HibernateTemplate template;
HibernateTransactionManager hbt;
public HibernateTransactionManager getHbt() {
return hbt;
}
public void setHbt(HibernateTransactionManager hbt) {
this.hbt = hbt;
}
public Login_DetailsDao() {
// TODO Auto-generated constructor stub
}
#Transactional
public void saveEmployee(Login_Details e){
System.out.println("Login_DetailsDao.saveEmployee()"+e.getMobileNo());
SessionFactory sf = hbt.getSessionFactory();
Session session =sf.getCurrentSession();
Transaction t =session.beginTransaction();
session.persist(e);
t.commit();
session.close();
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public List<Login_Details> getEmployees(){
List<Login_Details> list=new ArrayList<Login_Details>();
list=template.loadAll(Login_Details.class);
return list;
}
}
I am able to get the login_details data but unable to insert the record.
getting below exception
org.xml.sax.SAXParseException; lineNumber: 51; columnNumber: 29; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'.
Could any one please suggest to solve this?
Your code is flawed on multiple levels.
Never create a BeanFactory or ApplicationContext just because you need an instance of a bean. Instead use dependency injecting for that bean.
The fact that you use a BeanFactory over an ApplicationContext is one of the reasons your code doesn't work. See this for why you shouldn't use a BeanFactory.
You aren't using transactions and without transactions nothing will be persisted in the database.
Now first in your controller you need to dependency inject the Login_DetailsDao instead of creating a BeanFactory and obtain an instance.
#Controller
public class ControllerSignUp_Login {
private final Login_DetailsDao dao;
#Autowired // If using Spring 4.3 or later this isn't needed
public ControllerSignUp_Login(Login_DetailsDao dao) {
this.dao=dao;
}
}
Now in your method use this instance, instead of doing the lookup.
Your Login_DetailsDao should operate on the SessionFactory and nothing else.
public class Login_DetailsDao {
private final SessionFactory sf;
public Login_DetailsDao(SessionFactory sf) {\
this.sf=sf;
}
#Transactional
public void saveEmployee(Login_Details e){
sf.getCurrentSession().save(e);
}
#Transactional(readOnly=true)
public List<Login_Details> getEmployees(){
return sf.getCurrentSession()
.createQuery("SELECT ld FROM Login_Details", Login_Details.class)
.getResult();
}
}
That is all you need. Now finally in your XML tie all this together.
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:tx="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<constructor-arg ref="sessionFactory" />
</bean>
</beans>
One simple change made this application worked.
1 > Deleted applicationcontext.xml and added the contents in disapatcher-servlet.xml
disaptcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<tx:annotation-driven />
<!-- Provide support for component scanning -->
<context:component-scan base-package="com.uday" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<constructor-arg ref="mysessionFactory" />
</bean>
</beans>
DAO Class:
package com.uday;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
public class Login_DetailsDao {
private final SessionFactory sessionFactory;
public Login_DetailsDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public void saveEmployee(Login_Details e){
sessionFactory.getCurrentSession().save(e);
}
public boolean isLogoinSuccessfull(String uName , String password) {
List<Login_Details> a= getEmployees();
Iterator<Login_Details> i = a.iterator();
while(i.hasNext()) {
Login_Details ld = i.next();
String mobileNo = ld.getMobileNo();
String pas = ld.getPassword();
if(mobileNo != null && mobileNo.equals(uName) && pas != null && pas.equals(password)) {
return true;
}
}
return false;
}
#Transactional
public List<Login_Details> getEmployees(){
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Login_Details.class);
List<Login_Details> l = criteria.list();
return l;
}
}
Controller Class:
package com.uday;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class ControllerSignUp_Login {
#Autowired
private final Login_DetailsDao dao;
#Autowired
public ControllerSignUp_Login(Login_DetailsDao login_DetailsDao) {
this.dao = login_DetailsDao;
}
#RequestMapping("/hello")
#Transactional
public String diaplay(#RequestParam("name") String name, #RequestParam("pass") String pass,Model m) {
if(dao.isLogoinSuccessfull(name , pass)) {
m.addAttribute("message", "Hello"+name);
return "Success";
}
else {
m.addAttribute("message", "You have Entered Wrong pin");
return "Failure";
}
}
#RequestMapping("/SignUp")
public String redirect() {
System.out.println("ControllerSignUp_Login.display()");
return "signup";
}
#RequestMapping("/login")
public String display() {
System.out.println("ControllerSignUp_Login.display()");
return "login";
}
#RequestMapping("/updateDetails")
#Transactional
public String display(HttpServletRequest req , Model M) {
String firstName=req.getParameter("firstName");
String lastName=req.getParameter("lastName");
String mobileNo=req.getParameter("mobileNo");
String address=req.getParameter("address");
String city=req.getParameter("city");
String password=req.getParameter("password");
if(checkLength(firstName) && checkLength(lastName) && checkLength(mobileNo) && checkLength(address) && checkLength(city) && checkLength(password)) {
Login_Details ld = new Login_Details();
ld.setFirstName(firstName);
ld.setLastName(lastName);
ld.setCity(city);
ld.setAddress(address);
ld.setMobileNo(mobileNo);
ld.setPassword(password);
dao.saveEmployee(ld);
M.addAttribute("message", "SignUp Successfull !! Thank You");
M.addAttribute("displayLogin", true);
return "Success";
}
else {
M.addAttribute("message","SignUp Failed !! All details are mandatory.");
return "signup";
}
}
public boolean checkLength(String s) {
if(s != null && s.length() > 0) {
return true;
}
return false;
}
}
Project Structure looks like below
Instead of getting current session and making the transaction opened new session that's it worked like charm!!!
I don't know why it is not working if I am getting current session!!
#Transactional
public void saveEmployee(Login_Details e){
System.out.println("Login_DetailsDao.saveEmployee()"+e.getMobileNo());
SessionFactory sf = hbt.getSessionFactory();
//Changed here
Session session =sf.openSession(); //Session session =sf.getCurrentSession();
Transaction t =session.beginTransaction();
session.persist(e);
t.commit();
session.close();
}

How to catch Spring bean creation error - Injection of autowired dependency?

AdminService.java
package service;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.IAdminDAO;
import dao.IMemberDAO;
public interface AdminService
{
public HashMap<String, Object> adminLogin(String id,String pw);
}
AdminServiceImple.java
package service;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.IAdminDAO;
#Service
public class AdminServiceImple implements AdminService
{
#Autowired
private IAdminDAO adminDao;
#Override
public HashMap<String, Object> adminLogin(String id, String pw)
{
HashMap<String, Object> adminResult = adminDao.selectOne(id);
if(adminResult != null)
{
String opwd = (String) adminResult.get("pw");
if(opwd.equals(pw))
{
if(adminResult.get("authority").equals(true))
{
return adminResult;
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
}
AdminController.java
package controller;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.AdminService;
import service.AdminServiceImple;
import service.MemberService;
#Controller
public class AdminController
{
#Autowired
public AdminServiceImple adminService;
// 관리자 로그인 폼 페이지
#RequestMapping("admin.do")
public String adminLoginPage()
{
return "adminLoginPage";
}
// 관리자 로그인했을 시 요청
#RequestMapping("adminLoginOK.do")
#ResponseBody
public String adminMainPage(#RequestParam(required=false) String id, #RequestParam(required=false)String pw,HttpSession session,HttpServletRequest req,HttpServletResponse resp)
{
HashMap<String, Object> adminLoginIdentify = adminService.adminLogin(id, pw);
if(adminLoginIdentify != null)
{
return "1";
}
else
{
return "0";
}
}
#RequestMapping("adminPage.do")
public String adminPage(HttpSession session,HttpServletRequest resquest,HttpServletResponse response) throws IOException
{
return "adminMainPage";
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="dao, service" />
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property value="com.mysql.jdbc.Driver" name="driverClassName"></property>
<property value="jdbc:mysql://localhost/rachelvf" name="url"></property>
<property value="root" name="username"/>
<property value="mysql" name="password"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath:dao/mapper/*.xml"></property>
<property name="typeAliasesPackage" value="model"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperFactoryBean" id="memberDao">
<property name="mapperInterface" value="dao.IMemberDAO"></property>
<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperFactoryBean" id="adminDao">
<property name="mapperInterface" value="dao.IAdminDAO"></property>
<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
</bean>
</beans>
that is error code.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminServiceImple': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.IAdminDAO service.AdminServiceImple.adminDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.IAdminDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
I thought about the cause of the error, but I think it is because I did not insert the service annotation.
However, there is no typos in any way, and everything is written correctly and errors occur. Is there something I don't know?
Can you tell me what caused this error?
And what about the solution?
make sure that AdminDao bean is creating and injecting correctly into
AdminServiceImple
use this tag in your spring-cfg.xml file
<context:component-scan base-package="dao" />
and also scan the controller class using --
<context:component-scan base-package="controller" />
you have to give information of class which are going to use annotation to IOC container to create bean...
A mapper is registered to Spring by including a MapperFactoryBean in your XML config file like follows:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
try this to scan your Service Package and Dao Package.
<context:component-scan base-package="dao, service" />
above code will scan the dao and service package respectively.

HibernateException: No CurrentSessionContext configured (Hibernate4)

I have been working on various issues to get this Spring-Hibernate app to work. I have probably made a basic error.
This seems to be a recurring question with many answers covering older versions of Hibernate. I am using Spring 4.3.9 and Hibernate 4.0.5
The UT gets a SessionFactory but getCurrentSession() is returning null and in debug I can see that currentSessionContext is null.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="data.xml"/>
</beans>
data.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
>
<!-- Enable autowiring -->
<tx:annotation-driven/>
<!-- context:annotation-config/-->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="com.my"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.connection.pool-size">1</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
</beans>
SpringConfig.java
package utils.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
#Configuration
#Import
(
{SpringRepositoryConfig.class
}
)
#ImportResource("classpath:configuration/applicationContext.xml")
public class SpringConfig
{
}
SpringRepositoryConfig.java
package utils.config;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import utils.persistence.ItemRepository;
import utils.persistence.HibernateItemRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
// Declare as a configuration class
#Configuration
public class SpringRepositoryConfig
{
#Autowired
SessionFactory sessionFactory;
// Define repository bean
#Bean
public ItemRepository itemRepository()
{
ItemRepository rep = new HibernateItemRepositoryImpl();
rep.setSessionFactory(sessionFactory);
return rep;
}
}
SpringServicesConfig.java
package utils.config;
import javax.inject.Inject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import bussysutils.persistence.ItemRepository;
import bussysutils.repository.DecoderLoadRepository;
import bussysutils.repository.DecoderLoadRepositoryImpl;
#Configuration
public class SpringServicesConfig
{
#Inject
ItemRepository repository;
#Bean
public DecoderLoadRepository decoderLoadRepository()
{
DecoderLoadRepositoryImpl decoderLoad = new DecoderLoadRepositoryImpl(repository);
return decoderLoad;
}
}
SessionConfig.java
package utils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class SessionConfig
{
public static SessionFactory buildFactory(String url
,String user
,String password)
{
ServiceRegistry sr;
SessionFactory sfactory;
Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", url);
config.setProperty("hibernate.connection.username", user);
config.setProperty("hibernate.connection.password", password);
StandardServiceRegistryBuilder ssrb
= new StandardServiceRegistryBuilder().applySettings(config.getProperties());
sr = ssrb.build();
try
{
sfactory = config.buildSessionFactory(sr);
}
catch (Throwable ex)
{
throw new ExceptionInInitializerError(ex);
}
return sfactory;
}
}
UT_DecoderLoad.java
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import utils.DecoderLoad;
import utils.SessionConfig;
import utils.config.SpringConfig;
import utils.persistence.HibernateItemRepositoryImpl;
import utils.persistence.ItemRepository;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
#ContextConfiguration(classes = SpringConfig.class)
#RunWith(SpringJUnit4ClassRunner.class)
public class UT_DecoderLoad
{
#Autowired
SessionFactory sessionFactory;
#Test
public void decoderLoadTest()
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
sessionFactory = SessionConfig.buildFactory("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-host-name.my.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=dev)))"
,"myuser"
,"mypassword");
}
catch (SQLException e)
{
System.err.println("uploadServlet ERROR - " + e.getMessage());
System.out.print("uploadServlet logon ERROR - SQLException: " + e.getMessage());
e.printStackTrace();
}
Session s = sessionFactory.getCurrentSession(); /// <<<<<<<<
s.beginTransaction();
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
System.out.println(decoderLd.getSiteRefNo());
// update the database
ItemRepository itemRepo = new HibernateItemRepositoryImpl();
// itemRepo.create(decoderLd);
s.save(decoderLd);
s.getTransaction().commit();
}
}
You are making things very complicated for yourself, first you are mixing XML and Java based configuration and you have a configuration class for single beans. Either use Java or XML but don't mix them, especially if you aren't sure what those things do.
Your DataSource setup is also flawed as it is a partially setup DataSource.
Next your SessionFactory configuration is basically useless due to the use of the SessionConfig (which basically renders your Spring configuration useless). You shouldn't be using the SessionConfig, so drop it. Your SessionFactory configuration in the XML is also flawed, the hibernate.connection properties don't do anything, due the the injected DataSource and you shouldn't be messing around with the hibernate.current_session_context property unless you are using JTA. Spring will manage it for you.
Your unit test is also flawed, you should be injecting the repository not creating a new instance yourself.
That being said and moving everything to xml your data.xml should look something like this.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context">
<tx:annotation-driven/>
<context:property-placeholder location="jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.my"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernatTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="itemRepository" class="utils.config.HibernateItemRepositoryImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="decoderLoadRepository" class="utils.config.DecoderLoadRepositoryImpl">
<constructor-arg ref="itemRepository" />
</bean>
</beans>
The jdbc.properties would contain the following
jdbc.url=jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-host-name.my.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=dev)))
jdbc.username=myuser
jdbc.password=mypassword
You can now drop your SessionConfig, SpringServicesConfig and SpringRepositoryConfig classes as the first you shouldn't have in the first place the config class are obsolete due to adding the context to the xml file.
Now your test is also flawed as it should be #Transactional and should load the XML file instead of the java config.
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad {
#Autowired
SessionFactory sessionFactory;
#Test
public void decoderLoadTest() {
Session s = sessionFactory.getCurrentSession(); /// <<<<<<<<
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
System.out.println(decoderLd.getSiteRefNo());
s.save(decoderLd);
s.flush(); // "simulate a commit"
}
}
Although not sure what you are testing here, you should probably be testing the HibernateItemRepositoryImpl.
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad {
#Autowired
SessionFactory sessionFactory;
#Autowired
ItemRepository repository
#Test
public void decoderLoadTest() {
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
repository.create(decoderLd);
sessionFactory.getCurrentSession().flush(); // "simulate a commit"
// Validate the existence in the database
}
}
This is my current version, it runs but does not update the db table.
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
>
<!-- Enable autowiring -->
<tx:annotation-driven/>
<context:property-placeholder location="resources/jdbc.properties"/>
<bean id="DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<property name="packagesToScan" value="utils"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="itemRepository" class="utils.persistence.HibernateItemRepositoryImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="decoderLoadRepository" class="utils.repository.DecoderLoadRepositoryImpl">
<constructor-arg ref="itemRepository" />
</bean>
</beans>
UT_DecoderLoad.java
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import utils.DecoderLoad;
import utils.persistence.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad
{
#Autowired
SessionFactory sessionFactory;
#Autowired
ItemRepository itemRepo;
#Test
public void decoderLoadTest()
{
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
decoderLd.setDecoderNo("999");
System.out.println(decoderLd.getSiteRefNo());
itemRepo.create(decoderLd);
sessionFactory.getCurrentSession().flush();
}
}
I added a primary key column into the db table so that I could add the required #Id
DecoderLoad.java
package utils;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Table(name = "Decoder_Load")
public class DecoderLoad
{
public DecoderLoad()
{
}
// pojo with annotations
#Id
#Column(name = "decoder_no")
private String decoderNo;
...
I also added javassist to the pom.
Maven compile complains:
package org.springframework.test.context does not exist
package org.springframework.test.context.junit4 does not exist
This simple test runs but it is not inserting a record without forcing it and causing other issues.
EDIT: just learned that a test does a rollback so it will not commit.

when I am running test cases that time entity manager is injected successfully but while running web app its throwing NullPointerException

I have a strange problem. I am injecting the entity manager using PersistenceContext using applicatioContext bean. But problem is that when I am running test cases that time entity manager is injected successfully but while running web app its throwing NullPointerException
this is abstractRepository where I am injecting entityManager
package com.ajit.retail.repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
public class AbstractRepository {
#PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager entityManager;
}
this is my repository where I am using entity manager
package com.ajit.retail.repository;
import com.ajit.retail.exception.InvalidIdException;
import com.ajit.retail.model.Buyer;
import org.springframework.stereotype.Repository;
import javax.persistence.NoResultException;
#Repository
public class BuyerRepo extends AbstractRepository {
public Buyer getBuyer(String buyerName) throws InvalidIdException {
javax.persistence.Query buyerId = entityManager.createNativeQuery("select b.buyer_id from buyer b where b.name = :name").setParameter("name", buyerName);
Integer id;
try {
id = (Integer) buyerId.getSingleResult();
} catch (NoResultException e) {
return null;
}
return getBuyer(id);
}
public Buyer getBuyer(long buyerId) throws InvalidIdException {
Buyer buyer = entityManager.find(Buyer.class, buyerId); **====> NULL**
if (buyer == null) throw new InvalidIdException("Invalid Article ID");
return buyer;
}
}
this is my controller which is calling the repository method
package com.ajit.retail.web;
import com.sun.jersey.spi.inject.Inject;
import com.ajit.retail.exception.InvalidIdException;
import com.ajit.retail.model.Buyer;
import com.ajit.retail.repository.*;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/buyer")
#Component
public class BuyerController {
#Inject
private BuyerRepo buyerRepo;
#GET
#Produces(MediaType.APPLICATION_JSON)
public Buyer searchFields() throws InvalidIdException {
String buyerId = "51";
Buyer buyer;
try {
buyer = buyerRepo.getBuyer(Long.parseLong(buyerId));
} catch (NumberFormatException e) {
buyer = buyerRepo.getBuyer(buyerId);
}
return buyer;
}
}
these are the unit tests which are passing
package com.ajit.retail.repository;
import com.ajit.retail.exception.InvalidIdException;
import org.hamcrest.core.IsEqual;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.assertThat;
public class BuyerRepoTest extends BaseRepository {
#Autowired
private BuyerRepo buyerRepo;
#Test
public void shouldGiveNameOfGivenBuyerId() throws InvalidIdException {
assertThat(buyerRepo.getBuyer(2).getName(), IsEqual.equalTo("SupervisorDLS"));
}
#Test(expected = InvalidIdException.class)
public void shouldThrowExceptionForInvalidBuyerId() throws InvalidIdException {
buyerRepo.getBuyer(-10);
}
}
this is the base repository
package com.ajit.retail.repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
#ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BaseRepository extends AbstractTransactionalJUnit4SpringContextTests {
}
this is the applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.ajit.retail"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/retail"/>
<property name="username" value="retail_user"/>
<property name="password" value="password"/>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.ajit.retail"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Please Help!
I solved this problem by replacing
<servlet>
<servlet-name>retail</servlet-name>
**<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>**
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ajit.retail.web</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
with
<servlet>
<servlet-name>retail</servlet-name>
**<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>**
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ajit.retail.web</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Spring 3.1 JPA not inserting data while running in tomcat

I have spent three days trying to find the solution to this problem to no avail. I am desperate to figure this out. I have a simple spring app, running in servlet 2.5 with jstl tags 1.2, running in tomcat with spring 3.1, using hibernate and the hibernate jpa implementation.
I can list data from a page, but I cannot complete an insert. The record goes back, it appears to fire through with no problems. Yet no insert takes place. I know there are other posts similar but I have looked through them all and have not been able to find a solution anywhere.
If I run the exact same code via a MAIN class, the insert works fine. It just does not work when running as a web-app in tomcat.
I have tried running this via the main which works, inside the controller I have skipped calling the service layer, trying to go directly to the interface, when that didnt work, I tried going directly to the implementing DAO class, and that didnt work. It appears via the spring logs, that the entity manager is getting created, and shut down before a transaction takes place.
Please help me.
Here is my App-context
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.naturalbornliar.site"/>
<tx:annotation-driven />
<!-- Bean declarations go here-->
<bean id="duke" class="com.naturalbornliar.site.entity.Admin">
<constructor-arg name="admin_id" type="Long" value="15" />
<constructor-arg name="admin_login" type="String" value="testUser" />
<constructor-arg name="admin_pwd" type="String" value="testPwd" />
<constructor-arg name="email_id" type="int" value="15" />
<constructor-arg name="quote" type="String" value="Something to say here" />
</bean>
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> -->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"/> -->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/nbl_db"/> -->
<!-- <property name="username" value="web_user"/> -->
<!-- <property name="password" value="web_pwd"/> -->
<!-- <property name="initialSize" value="5"/> -->
<!-- <property name="maxActive" value="10"/> -->
<!-- </bean> -->
<bean id="simpledataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/nbl_db"/>
<property name="username" value="web_user"/>
<property name="password" value="web_pwd"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="simpledataSource"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="nblPersistenceUnit"/>
<property name="dataSource" ref="simpledataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>
<!-- <bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> -->
<!-- <property name="persistenceUnitName" value="nblPersistenceUnit"/> -->
<!-- <property name="dataSource" ref="simpledataSource"/>-->
<!-- <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> -->
<!-- </bean> -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* *..CategoryDaoImpl.*(..))" advice-ref="txAdvice"/>
</aop:config>
</beans>
Here is my controller:
package com.naturalbornliar.site.mvc;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.naturalbornliar.site.entity.Category;
import com.naturalbornliar.site.entity.Link;
import com.naturalbornliar.site.service.CategoryService;
#Controller
#RequestMapping("/categories")
public class CategoryController {
protected final Logger logger = Logger.getLogger(CategoryController.class);
private final CategoryService categoryService;
#Inject
public CategoryController(CategoryService categoryService){
this.categoryService = categoryService;
}
#RequestMapping(value="/listCategories")
public String listLinks(Model model){
model.addAttribute("categories", categoryService.getAllCategories());
return "categories";
}
#RequestMapping(method=RequestMethod.GET, params="new")
public String showCreateCategoryForm(Model model){
model.addAttribute(new Category());
return "addcategory";
}
#RequestMapping(method=RequestMethod.POST)
public String addCategoryFromForm(Category category, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return"addcategory";
}
categoryService.addCategory(category);
return "redirect:/categories/listCategories";
}
}
Here is my service called from the controller:
package com.naturalbornliar.site.service;
import java.util.Collection;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.naturalbornliar.site.entity.Category;
import com.naturalbornliar.site.i.ICategoryDao;
#Service
public class CategoryService {
private ICategoryDao iCategoryDao;
#Inject
public CategoryService(ICategoryDao iCategoryDao){
this.iCategoryDao = iCategoryDao;
}
public Collection<Category> getAllCategories(){
return iCategoryDao.getAllCategories();
}
public Collection<Category> getCategoriesByType(String type) {
return iCategoryDao.getCategoriesByType(type);
}
public Category getCategoryById(Long id) {
throw new UnsupportedOperationException();
}
public void deleteCategory(Category category) {
throw new UnsupportedOperationException();
}
public void updateCategory(Category category) {
throw new UnsupportedOperationException();
}
public void inactivateCategory(Category category){
throw new UnsupportedOperationException();
}
public void addCategory(Category category){
iCategoryDao.addCategory(category);
}
}
Here is my implementing DAO:
package com.naturalbornliar.site.dao;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.naturalbornliar.site.entity.Content;
import com.naturalbornliar.site.i.IContentDao;
#Transactional
#Repository
public class ContentDaoImpl implements IContentDao {
protected final Logger logger = Logger.getLogger(ContentDaoImpl.class);
#PersistenceContext
private EntityManager em;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
#Override
public void addContent(Content content) {
// TODO Auto-generated method stub
em.persist(content);
}
#Override
public void deleteContent(Content content) {
// TODO Auto-generated method stub
em.remove(content);
}
#Override
public void inactivateContent(Content content) {
// TODO Auto-generated method stub
}
#Override
public Content getContentById(Long id) {
// TODO Auto-generated method stub
return null;
}
#Override
public Content getContentByName(String name) {
// TODO Auto-generated method stub
return null;
}
#Override
public Collection<Content> getAllObjects() {
List<Content> resultList = em.createQuery("FROM Content", Content.class).getResultList();
return resultList;
}
}
Here is the interface (just in case)
package com.naturalbornliar.site.i;
import java.util.Collection;
import com.naturalbornliar.site.entity.Content;
public interface IContentDao {
public void addContent(Content content);
public void deleteContent(Content content);
public void inactivateContent(Content content);
public Content getContentById(Long id);
public Content getContentByName(String name);
public Collection<Content> getAllObjects();
}
Jeremy, thay may be many problems with that and I had similar problem.
In my case I used tomcat and I needed to add spring weaver to tomcat (this problem is described here: http://asrijaffar.blogspot.com/2007/02/spring-jpa-tomcat.html).
In my case I needed to have:
<tx:annotation-driven proxy-target-class="true" />
In DispatcherServlet config.
Additionally in db-context config:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jpatest" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Well I actually solved the problem. Here is the solution, there was another stackoverflow issue that solved it:
Spring #Transaction not starting transactions
Basically all that was needed for transactions to fire correctly was for me to add:
<tx:annotation-driven proxy-target-class="true"/>
to my servlet xml. I guess if you just have it in the application config.xml it only works when running under the main class (like in a standalone) if you need to run it in a container you have to declare transaction annotations in the servlet as well.
I also spent several hours trying to figure out where the problem is although my previous application with the same stack is working fine and I wasn't able to understand the difference.
And.... the error was in tag in spring-servlet.xml - it was defined to scan the root package with all the web controller, repository classes, etc.
After changing it to make scanning of the package with web controllers only, the problem was gone..
Just for you (and for me) in case you might meet the same problem, just an additional hint
I also had this same problem, spent couple nights searching - alex solutions saved me - in servlet xml I changed context:component-scan to scan only package with web controller.
Example from this page should look like
<context:component-scan base-package="com.naturalbornliar.site.mvc"/>

Resources