您好我的Spring / Hibernate项目有问题。我试图为DAO和服务实现泛型类,并使用一个具体的实现来在屏幕上显示某些内容。一切都是在没有错误的情况下开始的,但如果我想创建一个新项目,在表单子模块之后它会抛出Stack Overflow错误(见下图)。我无法找出问题所在。我希望这里有人可以帮助我。下面你可以看到我的所有代码,如果需要,可以添加jsp或配置文件。谢谢你的时间。
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";
}
}
答案 0 :(得分:1)
@Override
@Transactional(propagation = Propagation.REQUIRED)
public PK create(T t) {
return create(t);
}
此方法无条件地调用自身。这只会导致StackOverflowError。
你的意思是这样做吗?
@Override
@Transactional(propagation = Propagation.REQUIRED)
public PK create(T t) {
return genericDao.create(t);
}