0

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?

Vadym Borys
  • 115
  • 2
  • 11
  • Did you checked you sequence increment by value ? – NullPointerException Feb 16 '18 at 19:48
  • Id is automatically generated by hibernate `@Id @Column @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;` and I just do next stuff: get user object by id, set new name value of this object, save this user via saveOrUpdate and read user from db again. And every time ID is increased by 2. – Vadym Borys Feb 16 '18 at 21:04
  • You might me using IDENTITY columns then and its set to auto increment by 2 , check this [link](https://stackoverflow.com/a/11296469/715592) – NullPointerException Feb 17 '18 at 15:20

0 Answers0