-4

Hello I Have a problem with my Spring/Hibernate project. I was trying to implement generic classes for DAOs and Services and use one concrete implementation to show something on screen. Everything starts without error, but if i wanna create a new project, after form submisions it throws Stack Overflow error (see image below). I rly cant find out where the problem is. I hope someone here can help me. Below you can see all my code, potentialy can add jsp or config files if necessary. Thanks for your time.

enter image description here

GenericDaoImpl

@SuppressWarnings("unchecked")
@Repository
public abstract class GenericDaoImpl<T, PK extends Serializable> implements IGenericDao<T, PK> {

    @Autowired
    private SessionFactory sessionFactory;

    protected Class<? extends T> entityClass;

     public GenericDaoImpl() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        entityClass = (Class<? extends T>) pt.getActualTypeArguments()[0];
     }

    protected Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public PK create(T t) {
        return (PK) currentSession().save(t);

    }

    @Override
    public T read(PK id) {
        return (T) currentSession().get(entityClass, id);
    }

    @Override
    public void update(T t) {
        currentSession().saveOrUpdate(t);   
    }

    @Override
    public void delete(T t) {
        currentSession().delete(t);

    }

    @Override
    public List<T> getAll() {
        return currentSession().createCriteria(entityClass).list();
    }

    @Override
    public void createOrUpdate(T t) {
        currentSession().saveOrUpdate(t);   
    }

GenericServiceImpl

@Service
public abstract class GenericServiceImpl<T, PK extends Serializable> implements IGenericService<T, PK>{

     private IGenericDao<T, PK> genericDao;

    public GenericServiceImpl(IGenericDao<T,PK> genericDao) {
        this.genericDao=genericDao;
    }

    public GenericServiceImpl() {
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public PK create(T t) {
        return create(t);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public T read(PK id) {
        return genericDao.read(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(T t) {
        genericDao.update(t);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(T t) {
        genericDao.delete(t);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void createOrUpdate(T t) {
        genericDao.createOrUpdate(t);   
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<T> getAll() {
        return genericDao.getAll();
    }

}

ProjectDaoImpl

@Repository
public class ProjectDaoImpl extends GenericDaoImpl<Project, Integer> implements IProjectDao{

}

ProjectServiceImpl

@Service
public class ProjectServiceImpl extends GenericServiceImpl<Project, Integer> implements IProjectService {


    @Autowired
    public ProjectServiceImpl(@Qualifier("projectDaoImpl") IGenericDao<Project, Integer> genericDao) {
        super(genericDao);
    }

}

ProjectController

public class ProjectController {

     @Autowired(required = true)
     private IProjectService projectService;

    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public String listProjects(Model model){
        model.addAttribute("project", new Project());
        model.addAttribute("listProjects", projectService.getAll());
        return "project";
    }

    //for add and update role both
    @RequestMapping(value = "/project/add", method = RequestMethod.POST)
    public String addProject(@ModelAttribute("project") Project p){
        if( p.getId() == 0){
            //new role, add it
            projectService.create(p);
        } else {
            //existing role, call update
           projectService.update(p);
        }
        return "redirect:/projects";
    }

    @RequestMapping("/remove/{id}")
    public String deleteProject(@PathVariable("id") int id){
        projectService.delete(projectService.read(id));
        return "redirect:/projects";
    }

    @RequestMapping("edit/{id}")
    public String editProject(@PathVariable("id") int id, Model model){
        model.addAttribute("project", projectService.read(id));
        model.addAttribute("listProjects", projectService.getAll());

        return "project";
    }

}
Pogasta
  • 117
  • 1
  • 14
  • 1
    What do you think `public PK create(T t) { return create(t); }` does and why do you think so? Or asked differently: Why don't you think it is calling itself? How long do you have to sit there and stare at that line of code, which is **clearly highlighted by the error message**, to see that you probably forgot to qualify call with `genericDao.`? – Andreas Jan 19 '17 at 21:10
  • Possible duplicate of [What is a StackOverflowError?](http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – Joe C Jan 19 '17 at 21:32

1 Answers1

1
@Override
@Transactional(propagation = Propagation.REQUIRED)
public PK create(T t) {
    return create(t);
}

This method is calling itself unconditionally. This can only result in a StackOverflowError.

Did you mean to do this?

@Override
@Transactional(propagation = Propagation.REQUIRED)
public PK create(T t) {
    return genericDao.create(t);
}
Joe C
  • 15,324
  • 8
  • 38
  • 50