How can I get last inserted id using Hibernate

后端 未结 4 1952
醉话见心
醉话见心 2020-12-31 11:51

I want to fetch the last inserted value\'s id in Hibernate.

After search:

Long lastId = ((Long) session.createSQLQuery(\"SELECT LAST_INSERT_ID()\").         


        
4条回答
  •  一整个雨季
    2020-12-31 12:46

    public Integer save(Smartphones i) {
        int id = 0;
        Session session=HibernateUtil.getSessionFactory().openSession();
        Transaction trans=session.beginTransaction();
        try{
            session.save(i);   
            id=i.getId();
            trans.commit();     
        }
        catch(HibernateException he){}
        return id;
    }
    

提交回复
热议问题