当我创建一个新的鸡尾酒时,我想要返回那个鸡尾酒。我已经尝试了几种方法,但因此我是休眠的新手,并且 - 编程 - 我什至不确定我的语法是否正确......
我最新的代码片段 -->
我的鸡尾酒道:
// Return last created Cocktail
Session session;
public Cocktail getLastCocktail(){
return (Cocktail) session.createSQLQuery("Select LAST_INSERT_ID()").uniqueResult();
}
我的鸡尾酒控制器:
// Create Cocktail
@RequestMapping(value="/EditCocktail.html", method=RequestMethod.GET)
public ModelAndView edit(@RequestParam(required=false) Integer id) {
log.debug("EditCocktail.html");
ModelAndView mv = new ModelAndView();
if (id==null){
mv.addObject(new Cocktail());
} else {
mv.addObject(cocktailDao.findById(id));
}
mv.setViewName("edit-cocktail");
return mv;
}
//Get last created cocktail in order to continue with editing the cocktail
@RequestMapping(value="/CreateCocktail.html", method=RequestMethod.GET)
public ModelAndView lastCocktail(@RequestParam(required=false) Integer id) {
ModelAndView mv = new ModelAndView();
mv.addObject("title", "Cocktails");
mv.addObject("message", "Stellen Sie den Cocktail fertig");
mv.addObject("cocktail", cocktailDao.getLastCocktail());
System.out.println("_____________________________________________________________" + id);
mv.addObject("edit", false);
mv.setViewName("list-cocktail");
return mv;
}
现在我收到以下错误消息:
Jun 22, 2013 1:03:08 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DrinkersParadise] in context with path [/DP] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at de.hdu.pms.ctrl.CocktailController.lastCocktail(CocktailController.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
在此先感谢您的帮助!