简体   繁体   中英

How to work with session.merge in Hibernate?

I am now experiencing this problem. Using hibernate merge(Object o) method, it seems it is not working but the strange thing is on my other code it works. This is my codes looks like:

public Player addOrUpdate(Player player){
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try{
        tx = session.beginTransaction();
        if(player.getId() == null){
            id = (Integer) session.save(player);
        }else{
            session.merge(player);
            id = player.getId();
        }
        tx.commit();
     }//catch and finally
}

and on my Main Class, I am trying to do this:

PlayerDao pDao = new PlayerDao();
Player p = new Player();
p.setId(1);
p.setStatus("Hello");
pDao.addOrUpdate(p);

When I look at my database nothing change on my data. From my other code (Using other classes), I just did same and my data was updated successfully.

In general when using HibernateUtil.getSessionFactory().openSession() instead of .getCurrentSession() you should manually flush the objects.

So try calling this just before the transaction commit:

session.flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM