我是hibernate和spring框架的初学者。我想使用hibernate模板插入数据库。我的DAO代码:
public void save(News transientInstance) {
log.debug("saving News instance");
try {
save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
控制器
public void insert(NewsForm newsForm, RequestContext context) {
NewsDAO NewsDAO = (NewsDAO) ApplicationContext.getApplicationContext().getBean("NewsDAO");
News newNews = new News();
Timestamp date = new Timestamp(System.currentTimeMillis());
newNews.setDate(date);
newNews.setTitle(newsForm.getTitle());
newNews.setDescription(newsForm.getDescription());
newNews.setBody(newsForm.getBody());
newNews.setStatus(newsForm.getStatus());
newNews.setUpdateDate(date);
NewsDAO.save(newNews);
}
错误是
Caused by: java.lang.StackOverflowError
at org.apache.commons.logging.impl.Log4JLogger.debug(Log4JLogger.java:177)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68)
dao.NewsDAO.save(NewsDAO.java:68) ...etc
这个DAO代码错了吗?
答案 0 :(得分:2)
你的save方法调用save方法,它调用save方法,该方法调用save方法,该方法调用save方法,该方法调用save方法,该方法调用save方法,该方法调用save方法。 save方法,它调用save方法......
将save(transientInstance)
替换为hibernateTemplate.save(transientInstance)
。
(请尊重Java命名约定。变量以小写字母开头)。