hibernate - get id after save object

前端 未结 4 932
逝去的感伤
逝去的感伤 2020-12-02 20:04

Because of an purpose, I need to get an id of an object right after an insertion. I can work around with this code:

 session.save(Object o)   // insert to d         


        
4条回答
  •  甜味超标
    2020-12-02 20:40

    By default, hibernate framework will immediately return id , when you are trying to save the entity using Save(entity) method. There is no need to do it explicitly.

    In case your primary key is int you can use below code:

    int id=(Integer) session.save(entity);
    

    In case of string use below code:

    String str=(String)session.save(entity);
    

提交回复
热议问题