hibernate - get id after save object

前端 未结 4 912
逝去的感伤
逝去的感伤 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:18

    The session.save(object) returns the id of the object, or you could alternatively call the id getter method after performing a save.

    Save() return value:

    Serializable save(Object object) throws HibernateException
    

    Returns:

    the generated identifier
    

    Getter method example:

    UserDetails entity:

    @Entity
    public class UserDetails {
        @Id
        @GeneratedValue
        private int id;
        private String name;
    
        // Constructor, Setters & Getters
    }
    

    Logic to test the id's :

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();
    UserDetails user1 = new UserDetails("user1");
    UserDetails user2 = new UserDetails("user2");
    
    //int userId = (Integer) session.save(user1); // if you want to save the id to some variable
    
    System.out.println("before save : user id's = "+user1.getId() + " , " + user2.getId());
    
    session.save(user1);
    session.save(user2);
    
    System.out.println("after save : user id's = "+user1.getId() + " , " + user2.getId());
    
    session.getTransaction().commit();
    

    Output of this code:

    before save : user id's = 0 , 0

    after save : user id's = 1 , 2

    As per this output, you can see that the id's were not set before we save the UserDetails entity, once you save the entities then Hibernate set's the id's for your objects - user1 and user2

    0 讨论(0)
  • 2020-12-02 20:20

    Let's say your primary key is an Integer and the object you save is "ticket", then you can get it like this. When you save the object, a Serializable id is always returned

    Integer id = (Integer)session.save(ticket);
    
    0 讨论(0)
  • 2020-12-02 20:26

    or in a better way we can have like this

    Let's say your primary key is an Integer and object you save is "ticket", then you can get it like this. When you save the object, id is always returned

    //unboxing will occur here so that id here will be value type not the reference type. Now you can check id for 0 in case of save failure. like below:

    int id = (Integer) session.save(ticket); 
    if(id==0) 
       your session.save call was not success. 
    else '
       your call to session.save was successful.
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题