Why is INSERT created after em.persist in FlushModeType.COMMIT?

I set FlushModeType to COMMIT , but immediately after calling em.persist (), INSERT is executed in the database. I expect Hibernate to write changes to the database only at the end of the transaction, but it seems to work differently.

A small code to illustrate the problem:

@Entity
@Table(name="tbl1")
@Inheritance(strategy=InheritanceType.JOINED)
public class TopLevelEntity {}

@Entity
@Table(name="tbl2")
public class MyEntity extends TopLevelEntity { 
  AnotherEntity anotherEntity;      
  //getters and setters
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class AnotherEntity { }

public void someTransaction() {
  em.setFlushMode(FlushModeType.COMMIT);

  MyEntity e = new MyEntity();

  em.persist(e);

  e.setProp1(someVal1);
  e.setProp2(someVal2);

}

Here I expect the actual INSERT to be done at the end of the method immediately after calling setProp2 (). But Hibernate does the insert right after the call em.persist().

Can someone explain me this behavior?

Edit:
I even tried using FlushMode.MANUAL for a Hibernate session, but in any case, it does an INSERT after em.persist. So, is there a way to manually control when the flash is completed?

Edit2:
, . A, B. Entity B em.persist(a), " (B NOT NULL) ". , , Oracle. .

Edit3:
, . .

+5
3

FlushMode Commit , . ( , Hibernate), , . / bean, , . Cascade /, . / -, JPA, /dependecy null, , . , , - JPA. / , , , null ( , ), .

+3

COMMIT , . 3.2.4 :

FlushModeType.COMMIT, ; , , .

+1

Due to sleep mode firstlevelcache

0
source

All Articles