I've a transaction in Hibernate. I save an object usign session.save() and other using session.getNamedQuery().executeUpdate().
The problem is that the second object depends on existence of first, but even when both calls are on the same transaction, the first object "still doesn't exists" when I try to insert using the namedQuery.
If I flush() the session it works, but I don't now if it is a good practice to manipulate the session like that.
Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(myObjectToSave);
session.flush(); //only works with this line in between
session.getNamedQuery("ar.com.portal.bean.Application.insertLang").executeUpdate();
transaction.commit();
} catch (Exception e) {
if (transaction != null) transaction.rollback();
throw e;
}
Is that the correct solution? or that depends on some configuration parameter? I'm new to Hibernate.
Edit:
These are the properties that I've in my hibernate.cfg.xml
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
Contents of namedQuery
<sql-query name="insertLang" >
insert into APPLICATION_LANG (APPLICATION_ID, ISO_LANGUAGE_ID, WORKSPACE_ID, NAME, DESCRIPTION)
values ( :id, :lang, :systemObject.workspace.id, :name, :description)
</sql-query >