Please help with the below issue, Everyone seems to have the same kind of problem. I did read through the forum but couldn't find the solution. What ever solutions have been posted i tried couple of them
Removing the @Service declaration from serviceimpl class and declaring it in servlet-config.xml, but my project won't compile. Not sure what is causing this issue.
I am using STS tool, trying to insert the data in the table, but encountered below problem. If I remove the em.flush() then there is no exception but it doesn't save in the database. I have attahe entire package of mine for ref.
Exception
org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
root cause
javax.persistence.TransactionRequiredException: no transaction is in progress
org.hibernate.ejb.AbstractEntityManagerImpl.flush( AbstractEntityManagerImpl.java:983)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.ExtendedEntityManagerC reator$ExtendedEntityManagerInvocationHandler.invo ke(ExtendedEntityManagerCreator.java:365)
$Proxy25.flush(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
$Proxy25.flush(Unknown Source)
com.pluralsight.repository.GoalRepositoryImpl.save (GoalRepositoryImpl.java:24)
com.pluralsight.service.GoalServiceImpl.save(GoalS erviceImpl.java:21)
com.pluralsight.controller.GoalController.updateGo al(GoalController.java:43)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:219 )
org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:132)
org.springframework.web.servlet.mvc.method.annotat ion.ServletInvocableHandlerMethod.invokeAndHandle( ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.invokeHandleMetho d(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.handleInternal(Re questMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.Abstrac tHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:80)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
I tried adding @Transaction on my service method but I am still getting the above problem.
Src Files
GoalRepositoryImpl
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository{
@PersistenceContext
private EntityManager em;
public Goal save(Goal goal){
em.persist(goal);
em.flush();
return goal;
}
}
GoalServiceImpl
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
}
GoalRepositoryImpl .java
package com.pluralsight.repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transac tional;
import com.pluralsight.model.Goal;
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository{
@PersistenceContext
private EntityManager em;
public Goal save(Goal goal){
em.persist(goal);
em.flush();
return goal;
}
}
GoalServiceImpl.java
package com.pluralsight.service;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transac tional;
import com.pluralsight.model.Goal;
import com.pluralsight.repository.GoalRepository;
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
}
GoalController .java
package com.pluralsight.controller;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttri bute;
import org.springframework.web.bind.annotation.RequestMap ping;
import org.springframework.web.bind.annotation.RequestMet hod;
import org.springframework.web.bind.annotation.SessionAtt ributes;
import com.pluralsight.model.Goal;
import com.pluralsight.service.GoalService;
@Controller
@SessionAttributes ("goal")
public class GoalController {
@Autowired
private GoalService goalService;
@RequestMapping(value="addGoal",method=RequestMeth od.GET)
public String addGoal(Model model){
Goal goal = new Goal();
goal.setMinutes(45);
model.addAttribute("goal",goal);
return "addGoal";
}
@RequestMapping(value="addGoal",method = RequestMethod.POST)
public String updateGoal(@ModelAttribute("goal") Goal goal, BindingResult result) {
System.out.println("Minutes updated:"+goal.getMinutes());
if (result.hasErrors()){
return "addGoal";
}
else {
goalService.save(goal);
}
return "redirect:index.jsp";
}
}
Removing the @Service declaration from serviceimpl class and declaring it in servlet-config.xml, but my project won't compile. Not sure what is causing this issue.
I am using STS tool, trying to insert the data in the table, but encountered below problem. If I remove the em.flush() then there is no exception but it doesn't save in the database. I have attahe entire package of mine for ref.
Exception
org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
root cause
javax.persistence.TransactionRequiredException: no transaction is in progress
org.hibernate.ejb.AbstractEntityManagerImpl.flush( AbstractEntityManagerImpl.java:983)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.ExtendedEntityManagerC reator$ExtendedEntityManagerInvocationHandler.invo ke(ExtendedEntityManagerCreator.java:365)
$Proxy25.flush(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
$Proxy25.flush(Unknown Source)
com.pluralsight.repository.GoalRepositoryImpl.save (GoalRepositoryImpl.java:24)
com.pluralsight.service.GoalServiceImpl.save(GoalS erviceImpl.java:21)
com.pluralsight.controller.GoalController.updateGo al(GoalController.java:43)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:219 )
org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:132)
org.springframework.web.servlet.mvc.method.annotat ion.ServletInvocableHandlerMethod.invokeAndHandle( ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.invokeHandleMetho d(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.handleInternal(Re questMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.Abstrac tHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:80)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
I tried adding @Transaction on my service method but I am still getting the above problem.
Src Files
GoalRepositoryImpl
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository{
@PersistenceContext
private EntityManager em;
public Goal save(Goal goal){
em.persist(goal);
em.flush();
return goal;
}
}
GoalServiceImpl
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
}
GoalRepositoryImpl .java
package com.pluralsight.repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transac tional;
import com.pluralsight.model.Goal;
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository{
@PersistenceContext
private EntityManager em;
public Goal save(Goal goal){
em.persist(goal);
em.flush();
return goal;
}
}
GoalServiceImpl.java
package com.pluralsight.service;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transac tional;
import com.pluralsight.model.Goal;
import com.pluralsight.repository.GoalRepository;
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
}
GoalController .java
package com.pluralsight.controller;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttri bute;
import org.springframework.web.bind.annotation.RequestMap ping;
import org.springframework.web.bind.annotation.RequestMet hod;
import org.springframework.web.bind.annotation.SessionAtt ributes;
import com.pluralsight.model.Goal;
import com.pluralsight.service.GoalService;
@Controller
@SessionAttributes ("goal")
public class GoalController {
@Autowired
private GoalService goalService;
@RequestMapping(value="addGoal",method=RequestMeth od.GET)
public String addGoal(Model model){
Goal goal = new Goal();
goal.setMinutes(45);
model.addAttribute("goal",goal);
return "addGoal";
}
@RequestMapping(value="addGoal",method = RequestMethod.POST)
public String updateGoal(@ModelAttribute("goal") Goal goal, BindingResult result) {
System.out.println("Minutes updated:"+goal.getMinutes());
if (result.hasErrors()){
return "addGoal";
}
else {
goalService.save(goal);
}
return "redirect:index.jsp";
}
}