does session.flush() commit the changes to the DB????
what i am trying to do is, delete a set of records and then insert another set with the same primary key. if i don't do a session.flush() after the delete (i am using session.delete(String, Object[], Type[])), i get a primary key violation. if i do a session.flush(), everything works fine, but i just want to make sure, that the whole thing is executed in one transaction.
here is the sample code:
Code:
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
compatiblityDAO = new CompatiblityDAO(session);
//delete "TO" Program
compatiblityDAO.deleteCompatibilityTypes(toMarket, toSystemId);
session.flush(); //if i dont do this i get primary key violation
type= null;
//insert from "FROM" Program
for (Iterator it = fromTypes.iterator(); it.hasNext();) {
.... some processing
compatiblityDAO.insertCompatibilityType(type);
}
transaction.commit();
Here is the delete and insert methods of compatibilityDAO
Code:
private String SELECT_ALL_COMPATIBILITY_TYPES =
"from com.dcx.slsmkt.upic.rules.bo.CompatibleType as comp where comp.market= :market and comp.programId = :programId";
public void deleteCompatibilityTypes(String market, int systemId) throws RulesException {
try {
session.delete(SELECT_ALL_COMPATIBILITY_TYPES, new Object[] {market, new Integer(systemId)}, new Type[] {Hibernate.STRING, Hibernate.INTEGER});
} catch (HibernateException e) {
log.error(e);
throw new RulesException(e);
}
}
public void insertCompatibilityType(CompatibleType compType) throws RulesException {
try {
getSession().save(compType);
} catch (HibernateException e) {
log.error(e);
throw new RulesException(e);
}
}
Any help would be greatly appreciated. Thanks in advance.