Hibernate not saving object in database?

I have the following problem: when I try to insert an object into the Hibernate database, it gets stuck, the error does not return, the object is not saved correctly.

Debugging it, I found out that it hangs on

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 command works.

I'm not sure what I am missing. Also, using the built-in SQL Insert command, it works.

+1
source share
1 answer

I have not seen you doing the session

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

But most preferred 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();
    }
+4
source

Source: https://habr.com/ru/post/1661379/


All Articles