0

I've got the problem that my entity is inserted into the db without any "flush" being called, Maybe someone has an idea under which circumstances a session.save immediately triggers db insert?

I think the only to interesting parts of my object which I would like to save (but not persist(!)) is

  @javax.persistence.Id
  @javax.persistence.GeneratedValue(strategy = GenerationType.IDENTITY)
  @javax.persistence.Column(name = "EM_id")
  private Long     id;

  @javax.persistence.Version
  @javax.persistence.Column(name = "version")
  private int      version;
Thomas
  • 3
  • 1
  • When you save but don't persist what is the output? – Michael Nelles Jan 29 '20 at 10:29
  • The Object gets inserted into the DB, (i just create the object, and then I call session.save(object), nothing else) – Thomas Jan 29 '20 at 11:14
  • What do you expect `save()` to do? `save()` is used to write data into the database https://javarevisited.blogspot.com/2012/09/difference-hibernate-save-vs-persist-and-saveOrUpdate.html – XtremeBaumer Jan 29 '20 at 11:23
  • Up to now we've used hibernate with those xyz.hbm.xml files, now I've added a class which uses hibernate with annotations, objects of this annotation-using-class are now inserted to the DB, other objects (from classes which are configured via hbm.xml files) are only saved when I call "persist" (is there a way to configure the behaviour of "save"?) – Thomas Jan 29 '20 at 11:37
  • 1
    See this similar [question](https://stackoverflow.com/questions/27697810/hibernate-disabled-insert-batching-when-using-an-identity-identifier-generator) and the answer by @vlad-mihalcea. – Amit Mendapara Jan 29 '20 at 11:50
  • I forgot to mention that the FlushMode of all our sessions is set to FlushMode.MANUAL – Thomas Jan 29 '20 at 11:55

1 Answers1

0

Are you handling the transactions manually? If you are, you should start the transaction before calling save(). Commit the transaction after calling save(). I believe not starting the transaction might have been causing this.

If above is not the case, the saving without flush() is happening because of default hibernate flush mode being AUTO. Try changing it to COMMIT.

Sumit Desai
  • 1,542
  • 9
  • 22