We are using Hibemnate in our project.
In a perticulat table we have Blob field. When ever we are trying to insert a new row in the table we are getting two extra update statement in theat table with the same data.
The mapping file:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Dec 28 16:15:02 GMT+05:30 2005 -->
<hibernate-mapping package="com.unisys.ecs.server.hibernate">
<class name="ExternalDocuments" table="EXTERNAL_DOCUMENTS" >
<id name="externaldocumentcode" column="EXTERNALDOCUMENTCODE" type="java.lang.String">
<generator class="assigned"/>
</id>
<property name="externaldocumentdescription" column="EXTERNALDOCUMENTDESCRIPTION" type="java.lang.String" not-null="true" />
<property name="externaldocumentblob" column="EXTERNALDOCUMENTBLOB" type="java.sql.Blob" lazy="true" not-null="true" />
<property name="externaldocumentfilename" column="EXTERNALDOCUMENTFILENAME" type="java.lang.String" />
<!--
<property name="externaldocumenttype" column="EXTERNALDOCUMENTTYPE" type="java.lang.String" not-null="true" />
-->
<property name="lastupdated" column="LASTUPDATED" type="java.util.Date" not-null="true" />
<set name="procedureDocumentsSet" inverse="true">
<key column="EXTERNALDOCUMENTCODE"/>
<one-to-many class="ProcedureDocuments"/>
</set>
</class>
</hibernate-mapping>
The code for saving or update the object:
public Date setDocument (ExternalDocuments document,boolean isInsert) throws Exception {
Session session = null;
//Transaction tx = null;
Date lastUpdated = ECSUtilities.getDatabaseTimeStamp();
try {
session = HibernateSessionFactory.currentSession();
//tx = session.beginTransaction();
if(isInsert){
document.setLastupdated(lastUpdated);
session.save(document);
//session.isConnected();
session.flush();
//tx.commit();
System.out.println("ExternalDocuments saved successfully.......");
}else{
ExternalDocuments docFromDb = (ExternalDocuments)session.get(ExternalDocuments.class, document.getExternaldocumentcode());
if(docFromDb == null) {
throw new EntityNotFoundException();
}
else {
if(docFromDb.getLastupdated().after(document.getLastupdated()))
{
throw new StaleDataException();
}
else
{
if(document.getFile() == null)
{
document.setExternaldocumentblob(docFromDb.getExternaldocumentblob());
}
document.setLastupdated(lastUpdated);
session.evict(docFromDb);
session.flush();
session.update(document);
session.flush();
}
}
System.out.println("ExternalDocuments updated successfully.......");
}
return lastUpdated;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
Help me out.....
|