-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: delete and insert to asame table(1st delete then insert)
PostPosted: Sat Dec 09, 2006 8:37 am 
Newbie

Joined: Mon Nov 06, 2006 9:46 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

hibernate 3.1.2

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 ;

}


DB2 8.1





Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 09, 2006 12:17 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you might try to flush before you save.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 6:19 am 
Newbie

Joined: Mon Nov 06, 2006 9:46 am
Posts: 10
LaLiLuna wrote:
Thanks .it is working fine..Actually what is happening when we flush before save.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 8:11 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
shinoyvv2002 wrote:
Thanks .it is working fine..Actually what is happening when we flush before save.


When you flush, you ask Hibernate to synchronize the session (=first level cache) with the db. For example, if you add two objects to the session and activate the sql output, you'll see that no inserts is sent until commit.

If you ever flush before commit, the SQL calls being waiting are all sent to the DB.

To sum up :
* Flush() syncs your objects states with the db (or at least, tries to :p)
* clear() empties the cache. So pay attention if you do it before flushing that all data waiting to be synced won't be.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 11:22 am 
Newbie

Joined: Mon Nov 06, 2006 9:46 am
Posts: 10
Thanks for ur reply ...
Suppose if we have to send data to more than 2 tables(Eg: 5 or 6 tables

) in an order according to our program.Then to syncronise do we need to call flush before each save,update ,delete operations?or just call flush before commit..is it sufficient?


batmat wrote:
shinoyvv2002 wrote:
Thanks .it is working fine..Actually what is happening when we flush before save.


When you flush, you ask Hibernate to synchronize the session (=first level cache) with the db. For example, if you add two objects to the session and activate the sql output, you'll see that no inserts is sent until commit.

If you ever flush before commit, the SQL calls being waiting are all sent to the DB.

To sum up :
* Flush() syncs your objects states with the db (or at least, tries to :p)
* clear() empties the cache. So pay attention if you do it before flushing that all data waiting to be synced won't be.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 11:24 am 
Newbie

Joined: Mon Nov 06, 2006 9:46 am
Posts: 10
Suppose if we have to send data to more than 2 tables(Eg: 5 or 6 tables

) in an order according to our program.Then to syncronise do we need to call flush before each save,update ,delete operations?or just call flush before commit..is it sufficient?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 12:05 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
shinoyvv2002 wrote:
Thanks for ur reply ...
Suppose if we have to send data to more than 2 tables(Eg: 5 or 6 tables) in an order according to our program.Then to syncronise do we need to call flush before each save,update ,delete operations?or just call flush before commit..is it sufficient?

<parenthesis :-)>First, please preview your post before actually submitting. It will give a more readable thing : better for your chances to get an answer, and better for the users that will maybe search the archives in the future and find this post. Thanks</parenthesis>

Normally, you do not always need to call flush(). It's just sometimes useful to workaround problems like the one you met. Remember that commiting a transaction will flush your session, so there should be no need to call flush() explictly before commit.

(Also remember that session object should be short-lived, so trash it after each tx and create a new one, but this is maybe what you already do :-).)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 8:28 am 
Newbie

Joined: Tue Jul 20, 2004 4:35 am
Posts: 7
The problem I face is very similar:

the difference is that one of my object properties is collection (1-to-many association): some of collection members are to be deleted and some are new.

What is the best way to guarantee that all the "delete" operations will be executed before the "insert" ones?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 8:31 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
I guess there's no real way of doing this... Why do you want this? (Maybe also create a separate thread to treat this problem).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: Solved
PostPosted: Sat May 26, 2007 2:36 pm 
Newbie

Joined: Sat Nov 25, 2006 4:09 pm
Posts: 1
I had the same issue, but solved doing a hql delete statement to replace the session.delete(obj).

Before:

Code:
for(Iterator iter = oldtbdlvrs.iterator(); iter.hasNext(); ){
            
   Tbdlvr tbdlvr = (Tbdlvr) iter.next();
         
   session.delete(tbdlvr);
}


Now:

Code:
for(Iterator iter = oldtbdlvrs.iterator(); iter.hasNext(); ){
            
   Tbdlvr tbdlvr = (Tbdlvr) iter.next();
         
   session.createQuery(
                        "delete Tbdlvr where id.tbdijn=" + tbdlvr.getTbdijn() +
                        " and id.tbdunq=" + tbdlvr.getTbdunq() +
                        " and id.tbdlvl=" + tbdlvr.getTbdlvl() +
                        " and id.tbdfrm=" + tbdlvr.getTbdfrm()).executeUpdate();
}


and it worked fine, because it forces this delete statement to be executed before the next inserts and to 'change the default' flush sql statements order.

Thanks by the other ideas that came to me to think in this solution!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.