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.
It represents the classname of a custom ConnectionProvider which provides JDBC connections to Hibernate. It is used to set the JDBC transaction isolation level. It enables auto-commit for JDBC pooled connections.
Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.
Once save/update is done, the object DOES NOT reflect the change. The returned object reflects the changes, and it is attached to hibernate session. MERGE method offers greater flexibility when it comes to saving data objects, since you need not worry about attaching object to Session.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With