Hello,
I tried looking for an answer to the question I have but I wasn't able to find a straight forward answer anywhere.
Need a description?I have a table that has the following columns: Dept_Name, Dept_ID, Date_Tmst, Amount.
This table has a composite primary key - Dept_Name, Dept_ID, Date_Tmst.
There's an application that loads this table few times a day reading off of another table. And, at the end of the day this table gets purged (all the rows are deleted). When the application runs for the first time the next morning, it inserts rows for the departments reading off of another table. Second time onwards, the application has to just update the existing rows and if no row exists for a dept. it has to insert. The difference between the consecutive runs of the application is the Date_Tmst column value which changes every half-hour.
What did I try to do?I have a saveOrUpdateMany(IEntity entity, Collection collection) method to which I pass an entity which can be either oracle or DB2 and a collection of objects needed to be inserted/updated to the table.
What did I find?I found out that the
Code:
session.saveOrUpdate(object)
method always compares the primary key with the contents in the object.
Question?Inside the object passed as a parameter to session.saveOrUpdate(), the Dept_Name and the Dept_ID values will be the same but the Date_Tmst value would be changed to what is in the table. When the saveOrUpdate() method searches the table with the values in the object it won't find a row existing with those values. So, it performs an INSERT. But, I would like to update by just looking up at the Dept_Name and Dept_ID.
Is there a way to do that in Hibernate? Is HQL the way?
Code:
public static int saveOrUpdateMany(IEntity entity, Collection collection) throws HibernatePersistException
{
if (collection == null)
{
throw new HibernatePersistException("null parameter to update");
}
int numberObjects = 0;
if(collection.isEmpty())
{
return numberObjects;
}
Iterator iter = collection.iterator();
Session session = null;
try
{
session = getSession(entity);
Transaction tx = session.beginTransaction();
//prepare to insert or update many by sorting the entities by database
Map typeMap = prepareMany(iter);
//Itrerate through the mapping by database type and saveOrUpdate
iter = typeMap.keySet().iterator();
while(iter.hasNext())
{
String objectType = (String)iter.next();
Collection many = (Collection)typeMap.get(objectType);
if(many != null) {
Iterator objects = many.iterator();
while(objects.hasNext())
{
Object object = objects.next();
if (session == null)
{
if (!(object instanceof IEntity))
{
throw new HibernatePersistException("Invalid object to save or update, not an IEntity");
}
}
//saveOrUpdate the object
session.saveOrUpdate(object);
numberObjects++;
}
}
tx.commit();
//Flush transaction
flush(session, null);
closeSession(session);
}
}
catch(HibernatePersistException e)
{
// just rethrow this exception
throw e;
}
catch(Throwable e)
{
String message = "Error saving or updating many records into table: " + e.getMessage();
Log.error("12", HibernateDAO.class, message, e);
throw new HibernatePersistException(message, e);
}
return numberObjects;
Thanks.