Home Tutorialhelp Comment [b]hi, i want to delete some records from 1 t

 
 

Comment

shinoy v
[b]hi, i want to delete some records from 1 t,
December 9, 2006 at 6:14 PM

[b]hi,

i want to delete some records from 1 table and then i have to insert new one.I write the HQL for this like the method below.in this i wrote delete first and then insert(look at last section DELETE and SAVE operations only).but when committed it tries to perform insert first so constraint violation happens and DB2 state err coming.can u give an example for delete and insert inside 1 transaction to 1 table.I want it soon .I stuck with that problem in my project..





private void updateContact(CustomerContactForm customerContactForm,GDMSUserDTO roleUser)
{


Timestamp CurrentTS = new Timestamp(System.currentTimeMillis());
SessionFactory sf = null;
Session hibSession = null;
StringBuffer cusQuery=new StringBuffer();
StringBuffer cusQueryCmnt=new StringBuffer();
StringBuffer cusQueryChgLog=new StringBuffer();
StringBuffer cusQueryContDoc=new StringBuffer();

hibSession = HibernateUtil.getSessionFactory().openSession();
Transaction tx = hibSession.beginTransaction();
Long custContSkey=customerContactForm.getCustContSkey();//new Long(45);
try {
List custCont = new ArrayList();
cusQuery.append("from fit.gdms.schemadao.CustomerContact con ");
cusQuery.append("where con.custContSkey ="+custContSkey); //:custContSkey
custCont = hibSession.createQuery(new String(cusQuery)).list();

if (custCont.size()> 0)
{
CustomerContact custContact = (CustomerContact)custCont.get(0);
if(!(custContact.getCustContName()==customerContactForm.getContactName()))
{

custContact.setCustContName(customerContactForm.getContactName());
Customer customer=new Customer();
customer.setCustSkey(customerContactForm.getCustSkey());
custContact.setCustomer(customer);
Language language=new Language();
language.setLanguageCd(customerContactForm.getLanguageCd());
custContact.setLanguage(language);
ContactPosition contPosition=new ContactPosition();
contPosition.setContPosCd(customerContactForm.getPosition());//(contPosCd);
custContact.setCustContName(customerContactForm.getContactName());
custContact.setContactPosition(contPosition);
custContact.setContDept(customerContactForm.getDepartment());
custContact.setContEmail(customerContactForm.getEmail());
custContact.setContPhone1(customerContactForm.getPhone1());
custContact.setContPhone2(customerContactForm.getPhone2());
custContact.setContFax1(customerContactForm.getFax1());
custContact.setContFax2(customerContactForm.getFax2());
custContact.setCustConAddr1(customerContactForm.getLine1());
custContact.setCustConAddr2(customerContactForm.getLine2());
custContact.setCustConAddr3(customerContactForm.getLine3());
custContact.setCustConAddr4(customerContactForm.getPlaceCity());
custContact.setCustConAddr5(customerContactForm.getState());
custContact.setCustConAddr6(customerContactForm.getCountry());
custContact.setCustConAddr7(customerContactForm.getZipPostalCode());
if(("OTH").equalsIgnoreCase(customerContactForm.getPosition()))
{
custContact.setContPosDesc(customerContactForm.getPosDocdesc());

}

custContact.setLastUpdId(roleUser.getUserCNUM());
custContact.setLastUpdTs(CurrentTS);
hibSession.update(custContact);
}

List custContCmntList = new ArrayList();
cusQueryCmnt.append("from fit.gdms.schemadao.CustContCmnt cmnt ");
cusQueryCmnt.append("where cmnt.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custContCmntList = hibSession.createQuery(new String(cusQueryCmnt)).list();
if (custContCmntList.size()> 0)
{

CustContCmnt custContCmnt = (CustContCmnt)custContCmntList.get(0);
custContCmnt.setCmntLogText(customerContactForm.getComments());
custContCmnt.setCustomerContact(custContact);//.setCustomerContact(custContact);
custContCmnt.setLastUpdId(roleUser.getUserCNUM());
custContCmnt.setLastUpdTs(CurrentTS);
hibSession.update(custContCmnt);

}

List custCntChgLogList = new ArrayList();
cusQueryChgLog.append("from fit.gdms.schemadao.CustCntChgLog cccl ");
cusQueryChgLog.append("where cccl.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custCntChgLogList = hibSession.createQuery(new String(cusQueryChgLog)).list();
if (custCntChgLogList.size()> 0)
{

CustCntChgLog custCntChgLog = (CustCntChgLog)custCntChgLogList.get(0);
custCntChgLog.setCustomerContact(custContact);
custCntChgLog.setChgItem("CustomerContactName");
custCntChgLog.setLastUpdId(roleUser.getUserCNUM());
custCntChgLog.setLastUpdTs(CurrentTS);
custCntChgLog.setOldChgValue(custCntChgLog.getNewChgValue());
custCntChgLog.setNewChgValue(customerContactForm.getContactName());
hibSession.update(custCntChgLog);
}
List custContDocList = new ArrayList();
cusQueryContDoc.append("from fit.gdms.schemadao.CustContDoc ccd ");
cusQueryContDoc.append("where ccd.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custContDocList = hibSession.createQuery(new String(cusQueryContDoc)).list();
if (custContDocList.size()> 0)
{

for(int i=0;i<custContDocList.size();i++)
{
CustContDoc custContDoc = (CustContDoc)custContDocList.get(i);
hibSession.delete(custContDoc);

}
}
if(customerContactForm.getContDoc()!=null)
{

for (int i = 0; i< customerContactForm.getContDoc().length ;i++)
{
String[] contDoc = customerContactForm.getContDoc();

String contDocCd = contDoc[i];
CustContDoc custContDoc = new CustContDoc();
ContactDocmt contDocmt = new ContactDocmt();
contDocmt.setContDocCd(contDocCd);
Language lang=new Language();
lang.setLanguageCd(customerContactForm.getLanguageCd());
if (("OTH").equalsIgnoreCase(contDocCd) )
{

custContDoc.setContDocDesc(customerContactForm.getContDocDesc());

}
custContDoc.setLanguage(lang);
custContDoc.setContactDocmt(contDocmt);
custContDoc.setCustomerContact(custContact);
custContDoc.setLastUpdId(roleUser.getUserCNUM());
custContDoc.setLastUpdTs(CurrentTS);
hibSession.save(custContDoc);


}


}

tx.commit();
//hibSession.flush();
hibSession.close();
}
}catch (HibernateException e) {

tx.rollback();
System.out.println("err "+e);
e.getStackTrace();

}catch (Exception e) {
e.printStackTrace();


} finally {


//hibSession.close();
}
return ;

}[/b]

View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Advertisements Advertisements
 

 

 

DMCA.com