当我创建一个新鸡尾酒时,我想要返回鸡尾酒。我已经尝试了几种方法,但因此我不熟悉hibernate和 - 编程 - 我甚至不确定我的语法是否正确......
我的最新代码段 - >
我的鸡尾酒道:
// Return last created Cocktail
Session session;
public Cocktail getLastCocktail(){
return (Cocktail) session.createSQLQuery("Select LAST_INSERT_ID()").uniqueResult();
}
我的CocktailController:
// 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)
提前感谢您的帮助!
答案 0 :(得分:2)
使用Hibernate,您将获得保存方法,它将返回已保存对象的Id。保存后,只需获取ID,在数据库中查找,以便检索鸡尾酒。检查以下代码:
public Integer addCocktail(String yourProperties){
Session session = factory.openSession();
Transaction tx = null;
Integer cocktailID = null;
try{
tx = session.beginTransaction();
Cocktail mycockTail = new Cocktail(yourProperties);
cocktailID = (Integer) session.save(mycockTail);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return cocktailID;
}
希望它有所帮助!!
答案 1 :(得分:0)
好的,我的方法仍有问题,但它应该以某种方式工作:
CocktailDao:
// @SuppressWarnings("unchecked")
// public List<Cocktail> findLast() {
// Session session = getSession();
// String hql = "select MAX(tbl_cocktail.id) from tbl_cocktail";
// Query query = session.createQuery(hql);
// List<Cocktail> list = query.list();
// return list;
//
// }
CocktailController:
@RequestMapping(value="/CreateCocktail.html", method=RequestMethod.GET)
public ModelAndView findLast() {
ModelAndView mv = new ModelAndView();
mv.addObject("title", "Cocktails");
mv.addObject("message", "Stellen Sie den Cocktail fertig");
mv.addObject("cocktail", cocktailDao.findLast());
mv.addObject("edit", false);
mv.setViewName("list-cocktail");
return mv;
}