Hi, my problem is simple to explain: when I want to create/update/delete an item from any class it works when I do not use hibernate transactions! In addition, when I use transactions, no exception is thrown. Why could this be happening?
This code works:
Code:
try
{
Session sess = HibernateUtil.getSession();
Constant con = new con(name, code, ParentId);
sess.save(con);
HibernateUtil.flushSession();
}
catch(HibernateException e)
{
e.printStackTrace();
throw new BusinessException(ErrorMessage.INSERT_FAILURE);
}
finally
{
HibernateUtil.closeSession();
}
This one doesnt:
Code:
try
{
Session sess = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
Constant con = new con(name, code, ParentId);
sess.save(con);
HibernateUtil.commitTransaction();
}
catch(HibernateException e)
{
HibernateUtil.rollbackTransaction();
throw new BusinessException(ErrorMessage.INSERT_FAILURE);
}
finally
{
HibernateUtil.closeSession();
}
The big difference between both codes (besides the direct use of a transaction) is the flush after the object has been persisted. Isnt commit() supposed to do the flushing? (as it is illustred in the log excerpt below) or do I need to do it explicitly?
I'm using Jboss 3.0.6's JTA for transactions. Thanks for your help!
Hibernate version:2.1.6 Hibernate Service XML:Code:
<server>
<mbean code="net.sf.hibernate.jmx.HibernateService" name="jboss.jca:service=app.Hibernate">
<depends>jboss.jca:service=RARDeployer</depends><depends>jboss.jca:service=LocalTxCM,name=matriculasDS</depends>
<attribute name="MapResources">Constant.hbm.xml</attribute>
<attribute name="JndiName">java:/app.HibernateFactory</attribute>
<attribute name="Datasource">java:/appDS</attribute>
<attribute name="Dialect">net.sf.hibernate.dialect.Oracle9Dialect</attribute>
<attribute name="UseOuterJoin">false</attribute>
<attribute name="ShowSql">true</attribute>
<attribute name="TransactionStrategy">net.sf.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="TransactionManagerLookupStrategy">net.sf.hibernate.transaction.JBossTransactionManagerLookup</attribute>
<attribute name="CacheProvider">net.sf.ehcache.hibernate.Provider</attribute>
<attribute name="UseQueryCache">true</attribute>
<attribute name="QuerySubstitutions">true 'Y', false 'N', yes 'Y', no 'N'</attribute>
</mbean>
</server>
Mapping documents:Code:
<class
name="Constant"
table="CONSTANTS"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
>
<jcs-cache usage="read-write" />
<id
name="id"
column="ID"
type="java.lang.Long"
>
<generator class="increment">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="NAME"
length="100"
/>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CODE"
length="10"
/>
<many-to-one
name="parent"
class="Constant"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="CONS_ID"
/>
<set
name="children"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>
<cache
usage="read-write"
/>
<key
column="ID"
>
</key>
<one-to-many
class="Constant"
/>
</set>
</class>
The generated SQL (show_sql=true):NONE! Hibernate just fetches "select max(ID) from CONSTANTS", for the increment id generator!
Debug level Hibernate log excerpt:Code:
2004-11-08 13:09:08,709 DEBUG [com.edesa.matri.common.HibernateUtil] Opening new Session for this thread.
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: matriculas.HibernateFactory
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=4028803200195cd50100195cdee10000
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] opened session
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] loading [com.edesa.matri.persistence.Constant#1046]
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] attempting to resolve [com.edesa.matri.persistence.Constant#1046]
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.cache.ReadWriteCache] Cache lookup: 1046
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.cache.ReadWriteCache] Cache hit: 1046
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] resolved object in second-level cache [com.edesa.matri.persistence.Constant#1046]
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] creating collection wrapper:[com.edesa.matri.persistence.Constant.children#1046]
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] Cached Version: null
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.impl.SessionImpl] initializing non-lazy collections
2004-11-08 13:09:08,709 DEBUG [net.sf.hibernate.id.IncrementGenerator] fetching initial value: select max(ID) from CTE
2004-11-08 13:09:08,725 DEBUG [net.sf.hibernate.id.IncrementGenerator] first free id: 1048
2004-11-08 13:09:08,725 DEBUG [net.sf.hibernate.impl.SessionImpl] generated identifier: 1048
2004-11-08 13:09:08,725 DEBUG [net.sf.hibernate.impl.SessionImpl] saving [com.edesa.matri.persistence.Constant#1048]
2004-11-08 13:09:08,725 DEBUG [net.sf.hibernate.engine.Cascades] processing cascades for: com.edesa.matri.persistence.Constant
2004-11-08 13:09:08,725 DEBUG [net.sf.hibernate.engine.Cascades] done processing cascades for: com.edesa.matri.persistence.Constant
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.Cascades] processing cascades for: com.edesa.matri.persistence.Constant
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.Cascades] cascading to collection: com.edesa.matri.persistence.Constant.children
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.Cascades] done processing cascades for: com.edesa.matri.persistence.Constant
2004-11-08 13:09:08,741 DEBUG [com.edesa.matri.common.HibernateUtil] Committing database transaction of this thread.
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.transaction.JTATransaction] commit
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] flushing session
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushing entities and processing referenced collections
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] Processing unreferenced collections
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] Scheduling collection removes/(re)creates/updates
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] executing flush
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction before completion callback
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction before completion callback
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction after completion callback, status: 3
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction after completion callback, status: 3
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-11-08 13:09:08,741 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-11-08 13:09:08,756 DEBUG [com.edesa.matri.common.HibernateUtil] Closing Session of this thread.
2004-11-08 13:09:08,756 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
2004-11-08 13:09:08,756 DEBUG [net.sf.hibernate.impl.SessionImpl] disconnecting session
2004-11-08 13:09:08,756 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-11-08 13:09:08,756 DEBUG [com.edesa.matri.common.HibernateUtil] Session closed.
2004-11-08 13:09:08,944 DEBUG [net.sf.hibernate.impl.SessionImpl] running Session.finalize()
[/code]