Search code examples
javahibernatejpaexceptionentitymanager

Why is Hibernate's EntityManager.persist() not throwing an exception?


I cannot figure out why I am not seeing an exception for trying to insert 20 characters into a 10byte oracle table field... I confirmed the bad (too large string) is in the object, and the database does show varchar2(10).

Here is the code (extra logic removed for simplicity)

boolean isSuccess = false;
arrMyObject
for(int i = 0; i < arrMyObject.length; i++) {
    try {
        MyObject myObject = arrMyObject[i];
        em.persist(myObject);
        return true;
    } catch (Exception e) {
        System.out.println("Error");
        throw (e);
    }
}
return false;

I am not receiving an Exception (expecting some ORA exception). Nothing is being printed out, and I am receiving a TRUE value in my calling method. Also, nothing gets inserted to the table, which IS expected. With valid data I confirmed insert works.

Whats going on?


Solution

  • IMHO, at the point you test, the current transaction is not yet committed and the hibernate session is so not flushed yet, so you didn't reach the database and did not receive the error. Try to flush the hibernate session just to see (don't let the flush call in production)