0

当我创建一个新的鸡尾酒时,我想要返回那个鸡尾酒。我已经尝试了几种方法,但因此我是休眠的新手,并且 - 编程 - 我什至不确定我的语法是否正确......

我最新的代码片段 -->

我的鸡尾酒道:

// 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)

在此先感谢您的帮助!

4

2 回答 2

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;
   }

希望它有所帮助!

于 2013-06-24T20:24:25.020 回答
0

好的,我的方法仍然存在问题,但它应该像这样工作:

鸡尾酒道:

//  @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;
//
//  }

鸡尾酒控制器:

@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;
}
于 2013-06-24T19:49:53.073 回答