org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)

null id error in hibernate
org.hibernate.assertionfailure: non-transient entity has a null id:
nhibernate null id in
org hibernate assertionfailure hhh000099
nhibernate assertionfailure null identifier
org hibernate assertionfailure( interceptor onpreparestatement returned null or empty string)
don t flush the session after an exception occurs grails

I have a hibernate and JSF2 application going to the deployment server and suddenly throwing an org.hibernate.AssertionFailure: null id in exception. I will provide the stack trace and code immediately but here are four important issues first:

  1. This happens only on the deployment server (Jboss & MySql running on Windows Sever 2008.) It does not happen on my development machine (Tomcat and MySql running on Windoes 7 Pro) and also not on the staging environment (Jboss and MySql running on Linux.)

  2. Researching this, it seems that people get this error when trying to insert an object. But I get the error when I'm doing a simple query. (various different queries, actually, as the error pops up on several pages randomly.)

  3. The error hits only every now and then. If I do a Jboss restart it goes away, but a time later returns. Also, it's not consistent, on some clicks it's there, on others it's not. Even when it hits, when I do a simple refresh of the page it returns fine.

  4. I'm using c3p0 (config below)

Any idea what's going on?

The code details:

This happens on an address object. Here's the full hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.idex.auctions.model">
<class name="Address" table="address" lazy="true">
  <id name="addressID" column="AddressID">
        <generator class="native"/>            

  <property name="street" column="street"/> 
  <property name="city" column="city"/> 
  <property name="zip" column="zip"/> 
  <property name="state" column="state"/> 
  <property name="region" column="region"/> 
  <property name="country" column="country"/> 

  <many-to-one name="user" 

The Java class is straight forward:

public class Address implements Serializable {
private static final long serialVersionUID = 7485582614444496906L;

private long addressID;
private String street;
private String city;
private String zip;
private String state;
private String region;
private String country;
private User user;

public Address() {

public long getAddressID() {
    return addressID;
public void setAddressID(long addressID) {
    this.addressID = addressID;
public String getStreet() {
    return street;
public void setStreet(String street) {
    this.street = street;
public String getCity() {
    return city;
public void setCity(String city) { = city;
public String getZip() {
    return zip;
public void setZip(String zip) { = zip;
public String getState() {
    return state;
public void setState(String state) {
    this.state = state;
public String getRegion() {
    return region;
public void setRegion(String region) {
    this.region = region;
public String getCountry() {
    return country;
public void setCountry(String country) { = country;
public User getUser() {
    return user;
public void setUser(User user) {
    this.user = user;


The c3p0 configuration:

<property name="hibernate.c3p0.acquire_increment">1</property> 
<property name="hibernate.c3p0.idle_test_period">1000</property> 
<property name="hibernate.c3p0.max_size">20</property>  
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

The versions used are






The full stacktrace

org.hibernate.AssertionFailure: null id in com.idex.auctions.model.Address entry 
    (don't flush the Session after an exception occurs)
sun.reflect.GeneratedMethodAccessor350.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)

The exception:

org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)

Tells us that that the session exception has happened before the point where this org.hibernate.AssertionFailure is thrown.

To be exact, the org.hibernate.AssertionFailure is thrown when the session.flush() is happening, not the point where the error ocurred.

The above is a fact, thus a possible conclusion from it is: something could be suppressing the original exception.

So look for other possible points of error: A save() or saveOrUpdate() is possibly trying to persist an entity with a null field where, in the table, the column is NOT NULL?

TIP: To help in the debugging, try adding a session.flush() after every interaction with the Session object (e.g., session.merge(obj), etc.), this will hopefully cause the org.hibernate.AssertionFailure to happen earlier, closer to where the real problem is taking place. (Of course, after the debugging, remove those session.flush().)

In my case, the real exception was taking place inside a try/catch {} block where the catch suppressed the exception (didn't rethrow or warn me about it).

Caused by: org.hibernate.AssertionFailure: null | Content , AssertionFailure: null id in com.oms.hibernate.rms.mapping.User entry (don't flush the Session after an exception occurs) · ali1983 Feb  org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs) Tells us that that the session exception has happened before the point where this org.hibernate.AssertionFailure is thrown.

Hibernate: org.hibernate.AssertionFailure: null id don't flush the , org.hibernate.AssertionFailure: null id in com.comp123.db.domain.tables.​ReportEntry entry (don't flush the Session after an exception occurs). Re: Caused by: org.hibernate.AssertionFailure: null id in com.oms.hibernate.rms.mapping.User entry (don't flush the Session after an exception occurs) ali1983 Aug 22, 2009 3:07 PM ( in response to ali1983 )

org.hibernate.AssertionFailure: null id in entry (don't flush the , hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)'. To get rid of this problem, I cleared the  Hibernate: org.hibernate.AssertionFailure: null id don't flush the Session after an exception occurs

You are probably hitting some Hibernate bug. (I'd recommend upgrading to at least Hibernate 3.3.2.GA.)

Meanwhile, Hibernate does better when your ID is nullable so that Hibernate can always tell the difference between a new object that has not yet been persisted to the database and one that's already in the database. Changing the type of addressID from long to Long will probably work around the problem.

The stack trace you provided shows that you are seeing the problem on a query because your query is forcing buffered writes to be flushed to the database before the query is executed and that write is failing, probably with the same insert problem other people are seeing.

Hibernate Community • View topic, AssertionFailure: null id in entry (don't flush the Session after an exception occurs) at org.hibernate.event.def. Grails/GORM : org.hibernate.AssertionFailure: null id in xyz (don't flush the Session after an exception occurs) -

I was facing this issue I just add try catch block and in catch block I wrote seesion.clear(); now I can proceed with the rest of records to insert in database.

org.hibernate.AssertionFailure: null id in entry (don , org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs). Posted by: admin  one use case that is not cleanly handled is a poller of some kind. If an application is setting some "in progress status" for a large background job, commits, then separate thread/http request/session polling, while an async background thread throws an exception, that async thread needs to be responsible to catch that exception and mark the status as "failed" on the thing the poller is polling.

org.hibernate.AssertionFailure, AssertionFailure: null id in persisttest.Publication entry (don't flush the Session after an exception occurs) at org.hibernate.event.def. Spring JPA/Hibernate org.hibernate.AssertionFailure: null id in Entity (don't flush the Session after an exception occurs) Ask Question Asked 5 years, 3 months ago

org.hibernate.AssertionFailure: null id /, org.hibernate.AssertionFailure: null id org.hibernate.AssertionFailure: null id in cn​.allobject.db.AReview entry (don't flush the Session after an exception occurs)  2008-12-28 15:46:30,481 ERROR [org.hibernate.AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: null id in de.kwizzard.model.dataobject.DOQuestion entry (don't flush the Session after an exception occurs)

JPA id null (field doesn't seem to be, being autogenerated), org.hibernate.AssertionFailure: null id in models.Payment entry (don't flush the Session after an exception occurs) at org.hibernate.event.def. Grails-null id in com.easytha.Student entry (don't flush the Session after an exception occurs) 0 Facing exception “org.hibernate.PersistentObjectException: detached entity passed to persist” when I try insert data in the database

  • If all else fails, you could take a look at the org.hibernate.event.def.DefaultFlushEntityEventListener.checkId() method and trace back where the id that is found null coming from, then find out how to set it/ensure it is not null. If the problem is indeed due to the Address class, you can put in a break point for where the id is set (maybe a null id is passed). The call is triggered when a flush occurs (which would explain why it is "random": when an auto-flush is required), so you might also want to check on (Address?) objects that have not been flushed yet.
  • Thanks for this, Attila. Can you please expand on how to get the org.hibernate.event.def.DefaultFlushEntityEventListener.checkId() method to help me, and how to check on Address objects that hasn't been flushed yet?
  • Actually how can the address id even be null, when it's a primitive long?
  • I could not find the source for 3.0, but the 3.2 source is available from Specifically: DefaultFlushEntityEventListener - The exception is thrown, when the id parameter of checkId() is null. You will need to see where that id is comming from (start from the stack trace you provided to see how checkId() is called). The easiest way is to download the whole source and do rearches there.[cont]
  • [cont]Note that there might be slight differences in the downloaded code and the library you are using as they have different versions (maybe get a copy of the 3.2 version of the Hibernate library to ensure you are looking at the code that is actually being executed)
  • How do you fix this? I am trying to save many entities, in a loop. Some of which may fail. How can I get the successful records to persist?
  • @DavidWilliams You should have each save in its own transaction, then.
  • I have the same problem and I'm doing it in separate transactions but the problem is still there. What should I do to be able to save next items in DB using the same ISession object?
  • Perhaps this is a good opportunity to take notice of how absolutely dangerous empty catch blocks are, they can take weeks of time to troubleshoot! Always have something in there that brings it to the developers attention, at least a .printStackTrace. If you are absolutely sure you want it to be empty (Checked exceptions CAN lead to such a situation), justify this in comments.
  • Thanks, Yves. Can you expand on the DataSource with connection pooling, so I will understand why do you suggest removing c3p0?
  • Also, I'm not using the open session in vew pattern. All my hibernate code looks like this: public static List<Auction> findAllAuctions() { String sql = "FROM com.auctions.model.Auction order by startDate desc"; Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<Auction> auctions = (List<Auction>)session.createQuery(sql).list(); session.getTransaction().commit(); return auctions; }
  • I have to investigate if such usage may lead to session sharing between threads...
  • I propose to remove c3p0 because Tomcat and JBoss already has pooling feature delivered by JavaEE DataSource. Tomcat uses dbcp by default. My idea is just to remove a component that duplicates a feature already available in your app server stack.
  • Thanks for the c3p0 explanation. I will remove it.