6

I have the following problem, when trying to insert an object in the database Hibernate gets stuck, no error returned, the object is not saved correctly.

Debugging it I found out that it hangs at

Hibernate: select nextval('hibernate_sequence')

I am using PostgreSQL as a database.

My configuration file:

<hibernate-mapping>
    <class name="src.MyClass" table="MyClass">

    <cache usage="read-write"/>

    <id name="id" column="classid">
        <generator class="native" />
    </id>

    <property name="name" column="name" not-null="true" unique="true" length="160"/>

</class>
</hibernate-mapping>

@Override
public void save( Myclass mc)
{
    Session session = sessionFactory.getCurrentSession();

    session.save( mc);
}

The SELECT part works.

I'm not sure what I'm missing. Also using native SQL Insert command, it works.

Mythul
  • 1,807
  • 7
  • 34
  • 53
  • 2
    OMG, please use annotations. Don't waste y/ou/r time with xml when JPA 2 exist :) – David Hofmann Aug 02 '13 at 15:25
  • 1
    Is the sequence 'hibernate_sequence' exists? – Luca Basso Ricci Aug 02 '13 at 15:25
  • 1
    You need to be sure that you begin() and commit() (or was it flush() after save() in Session?) a transaccion inside your Session object for your save() to work – David Hofmann Aug 02 '13 at 15:25
  • 1
    Yes 'hibernate_sequence' exists. I am using xml because the project is older and I need to respect its configuration. – Mythul Aug 02 '13 at 15:41
  • Still,if possible you can push management to move to latest version,which reduces the pain – Suresh Atta Aug 02 '13 at 18:33
  • I had exactly the same symptoms, but wrongly configured transaction manager for the Spring JpaRepository. So [moving from DataSourceTransactionManager to JpaTransactionManager](https://stackoverflow.com/questions/42587328/javax-persistence-transactionrequiredexception-no-transaction-is-in-progress/42591919#42591919) did the job. – imy Dec 23 '20 at 10:46

1 Answers1

12

I did'nt see you that flushing your session

Session session = sessionFactory.openSession();
session.save(mc);
session.flush();
session.close();

But most preferable is

Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        session.save(mc);
        tx.commit(); // Flush happens automatically
    }
    catch (RuntimeException e) {
        tx.rollback();
        throw e; // or display error message
    }
    finally {
        session.close();
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • It worked with flush. Lol I've wasted a lot of time on this issue. At least I've learned how to debug with Hibernate... Thanks – Mythul Aug 02 '13 at 15:57
  • Thank you very much! Calling session.flush() helped! What is interesting, when using MySQL everything worked without explicite flushing just fine. I needed this for the issue I described here: http://stackoverflow.com/questions/40708288/atomikos-data-not-getting-saved-when-using-postgresql – informatik01 Nov 21 '16 at 13:37