Hibernate merge

后端 未结 3 1531
孤城傲影
孤城傲影 2021-02-04 12:35

I am testing hibernate and giving this query to

transaction = session.beginTransaction();
city = new City(\"A\");
city  = (City)session.merge(city);
city.setNam         


        
3条回答
  •  遥遥无期
    2021-02-04 13:00

    It's a sequencing issue. Actually not an issue. Hibernate is doing exactly what you told it to do. As @TejasArjun says, merge is about merging in deteched data. here's what is going on:

    ...
    city  = (City)session.merge(city); 
    // No different to save(). Hibernate schedules an insert to the
    // database to store the current record.
    
    city.setName("B"); 
    // The object is now flagged as dirty and needing to be saved. 
    // Hiberate automatically tracks properties on objects and knows when they change.
    
    transaction.commit(); 
    // Hibernate sees that 'city' has been changed since it was saved, 
    // so it schedules an update to store the new data.
    

提交回复
热议问题