Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:27:15+00:00 2026-05-26T08:27:15+00:00

I have set FlushModeType to COMMIT , but right after em.persist() call, the INSERT

  • 0

I have set FlushModeType to COMMIT, but right after em.persist() call, the INSERT is made to database. I expect Hibernate to write changes to database only at the end of transaction, but it seems to work differently.

Little 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, that the actual INSERT will be made at the end of method, right after setProp2() call.
But Hibernate does the insert right after calling em.persist().

Can somebody explain me this behaviour?

Edit:
I even tried using FlushMode.MANUAL for Hibernate Session, but it performs INSERT after em.persist anyway. So, is there no way to manually control when flush is done?

Edit2:
Here is the issue, which I’m facing. I have an entity A which has-a entity B. Entity B doesn’t exist yet during the call to em.persist(a), and I have a ‘CONSTRAINT CHECK (B IS NOT NULL) in the database`. The only workaround which I found is to use deferred constraint in Oracle. However, this solution is vendor-specific and doesn’t really satisfy me.

Edit3:
Cascade seems like a good solution, but in our project we’re not allowed to use it. We’re encouraged to manually control all the inserts and updates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:27:15+00:00Added an answer on May 26, 2026 at 8:27 am

    FlushMode Commit does not guarantee that the commit will happen at transaction commit. It is more of a hint for the provider (implementator of specs, like Hibernate here), but he can do it whenever he chooses. If you want to persists/update a bean which has a new dependency that doesn’t exist yet in the database. use Cascade on persists/merge operations to persist it as well. IF you declared a field/dependency as not-nullable than it’s normal for JPA not to let you save an entity with that field/dependecy null, as it enforces the constrains you put on it. What you want to do there is pretty much a hack/workaround around the JPA specs. you either make that field/dependency nullable if in fact it can sometimes be null, or you make sure that it’s never null (and cascaded if it’s new value) when you persist the parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set the FlushMode property on an NHibernate session to FlushMode.Never, but when
I have set the itemRollOver and itemRollOut event listeners on a List component, but
I have set a list of items in a combobox, but when I debug
I have set a background image in my app, but the background image is
I have set up a few rules based on other questions but now my
I have set up an ASP.NET MVC project, and everything is working great, but
I have set file permissions to 777 yet I cannot write to the file
I have set up an NSTimer which after one second should perform a instance
I have set textof lablel to dropdownlist.selectedvalue in javascript. But when I try to
I have set the title for navigation item on top of the screen. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.