I've created method to save entity with new id or update existing via hibernate session. When I use next code:
try {
Session session = sessionWrapper.getSession();
sessionWrapper.beginTransaction();
if (user.getId()==null || session.get(User.class, user.getId())==null) {
return (long) session.save(user);
} else {
session.get(User.class, user.getId());
session.merge(user);
return user.getId();
}
} finally {
sessionWrapper.commit();
sessionWrapper.closeSession();
}
it works ok, but when I use session.saveOrUpdate instead in case of new entity generated id is increased by two, not one. Why and how to fix it?