-->
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.  [ 13 posts ] 
Author Message
 Post subject: How to update the existing persisted class
PostPosted: Wed Sep 29, 2004 12:57 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi ,

Could any one please tell me , how can I update a persistent class which have many fields updated (not known , how many fields has been changed).

I just want to update the existing record with the new updated one,considering all fields might have updated.

The one approach can be , loading the object and then setting each fields
but doesn't seems a clever way to doing it.

Please help me out..


Thanks & Regards,
Irfa


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 12:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
session.update() ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 10:03 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
michael wrote:
I have used that one , but It's throwing Exception "StaleObjectStateException" and saying some other transaction are trying to delete /update the record. Either unsaved-value has not been set properly.

After that , I have used the saveOrUpdate and added the unsaved-value="null" into the mapping file, and now , It's started inserting a new row into the table, while I'm expecting the update will take place as it has
existing identifire.

Could you please advise , why it's happening.



Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 10:08 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I think you have to go back to the documentation before you make more changes to your code.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 10:43 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Quote:
Hi Bauer,

My requirement is just to update the existing record by the new one as I know that always I've to update.

I have gone through your book "Hibernate in Action" , but doesn't seems to help.

Could you please tell me , why it's happening.

I have already spent more time by looking on net , but no solution yet.

Thanks & Regards,
Irfan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 10:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I don't know what "update a persistent class means" in this context. Usually this means writing code. I think you mean "update a persistent object". That should be really straightforward. If you don't understand it, chapter 2 of HiA has a very simple Hello World. And yes, loading the object and setting new attribute values is very clever way to do that.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 11:03 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Thanks for prompt response !

I want to update the persistent object as a whole and not by setting each and every attributes of the object as the object has at least 30 attributes and I don't know which attribute has changed.

For a known number of updated attribute , it's working fine. But when I'm trying to replace the persistent object in database by the new one (with updated value) having the same identifier it's throwing exception "StaleObjectException".

Is there any other way to update by using Session.update(),
saveOrUpdate() , or the only way , load the persistent object and then set all the attributes.


Thanks & Regards,
Irfan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 11:05 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I still don't understand what you are trying to do, but what I know is that this is all very well explained in the first chapters of HiA and of course in the reference and any tutorial about Hibernate that is around.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 11:19 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi Bauer,

Let me make it very simple. I have a persistent object and on UI , I'm displaying all the attributes , and now user hjas changed some of the attributes and trying to save.

In this case I have to update the persisted object the modified one.
I'm wondering , if there is only way just load the persistent object and the modify each and every attributes.

Please let me know , if I'm able to explain my problem to you.

Thanks & Regards,
Irfan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 01, 2004 2:11 pm 
Newbie

Joined: Sun May 02, 2004 2:52 pm
Posts: 5
IrfanAnsari wrote:
I have a persistent object and on UI , I'm displaying all the attributes , and now user hjas changed some of the attributes and trying to save.

In this case I have to update the persisted object the modified one.
I'm wondering , if there is only way just load the persistent object and the modify each and every attributes.


This would be easier if you had provided a code snippet, but I think I hear that you are attempting the following:

    1. Loading and object (cat).
    2. Send (cat) to the user.
    3. User modifies (cat) then passes back.
    4. Loading a second instance of same object (secondCat)
    5. Copy field for field, including id (cat) to (secondCat)
    6. Save (secondCat)

This is a classic detached objects scenario and the documentation is quite clear on how to do this. It is not necessary to reload the cat and do the field by field copy. A simple update() on the original cat would suffice.

If you are performing this work all with in the same Session, then it is even easier: (straight form the manual):


Code:
Cat fritz = (Cat) sess.load(Cat.class, id);
// pass it out to UI and modify fritz, then send back
sess.flush(); // persist changes to DB.


Hope that helps,
John


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 8:02 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi John,

You got the my point and here is the code snippet.

private void updatePromotionRuleHibernate(PromotionRule promotionRule , User user) throws HibernateException , PersistenceException
{
Session hibernateSession = openSession();
Transaction transaction = null;
PromotionRuleAuditEntry auditEntry;
Promotion prom = promotionRule.getPromotion();

float discount =0;
int fullPriceNights=0 ;
int discountPriceNights =0 ;

try{
transaction = hibernateSession.beginTransaction();
//load the promotion for the given promo_id

PromotionRule rule = (PromotionRule) hibernateSession.load(DefaultPromotionRule.class,new Integer(promotionRule.getID()));

if (prom instanceof DefaultDiscountStayPromotion){
discount = ((DefaultDiscountStayPromotion)prom).getDiscount();
fullPriceNights = ((DefaultDiscountStayPromotion)prom).getFullPriceNights();
discountPriceNights =((DefaultDiscountStayPromotion)prom).getDiscountPriceNights();
}
List promo = hibernateSession.find(FIND_PROMOTION, new Object[]{new Double(discount),new Integer(fullPriceNights),
new Integer(discountPriceNights)},new Type[]{Hibernate.DOUBLE,Hibernate.INTEGER,Hibernate.INTEGER});

//If promotion already exist then get the get the promotion id and set the promotion id for promotion attached to promotion rule and that will make the isPersisted method to return true.
if(promo.size() >0){
for (Iterator i= promo.iterator();i.hasNext();){
Promotion promotion =(Promotion) i.next();
promotionRule.getPromotion().setID(promotion.getID());
promotionRule.setPromotion(promotion);
}
}

if(! promotionRule.getPromotion().isPersisted()){

int promoTypeID = prom.getPromotionType().getID();
int promoLayerID= prom.getPromotionLayer().getID();
int promoTypeLayerID = DEFAULT_INT ;
//List the promo type layer for the given layer attached in the promotion. There always should be an entry in the promotion_type_layer table
// against a promo_layer_id and hard coded promo_type_id.

List promoTypeLayer = hibernateSession.find(FIND_PROMOTION_TYPE_LAYER, new Object[]{new Integer(promoTypeID),
new Integer(promoLayerID)},new Type[]{Hibernate.INTEGER,Hibernate.INTEGER});

//Get the promo_type_layer_id to set into promo

for (Iterator i = promoTypeLayer.iterator();i.hasNext();){
PromotionTypeLayer typeLayer = (PromotionTypeLayer) i.next();
promoTypeLayerID = typeLayer.getID();
promotionRule.getPromotion().setPromotionTypeLayerID(promoTypeLayerID);
}
log.debug("Creating a new Promotion ");
hibernateSession.save(promotionRule.getPromotion());
log.debug("New opromotion saved.");
promotionRule.setPromotion(promotionRule.getPromotion());
}

// promotionRule =updatePromotionRuleObject(rule , promotionRule);
log.debug("Setting the current promotion rule in the transient object");
rule.setPromotionRule(promotionRule);
log.debug(" the current promotion rule in the transient object has been set");
hibernateSession.update(rule);
log.debug("The new rule updated in the database.");

auditEntry = new DefaultPromotionRuleAuditEntry(PromotionRuleAuditEntry.UPDATED, promotionRule, user.getId());
hibernateSession.save(auditEntry);
transaction.commit();
}catch(Exception e){
if(transaction !=null){
transaction.rollback();
}
throw new PersistenceException("Unable to update a PromotionRule", e);
}
finally {
closeSession(hibernateSession);
}


}


Could you please see that where I'm wrong.

Thanks & Regards,
Irfan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 1:44 pm 
Newbie

Joined: Sun May 02, 2004 2:52 pm
Posts: 5
At first glance, I would say that that there is no reason to load 'rule' and copy 'promotionRule' into it. The following is unnecessary:
Code:
//    load the promotion for the given promo_id
PromotionRule rule =(PromotionRule) hibernateSession.load(DefaultPromotionRule.class,new Integer(promotionRule.getID()));


Try the following:



-John

_________________
John Sampson


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 1:51 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi John,

I tried the following code and this works.

private void updatePromotionRuleHibernate(PromotionRule promotionRule , User user) throws HibernateException , PersistenceException
{
Session hibernateSession = openSession();
Transaction transaction = null;
PromotionRuleAuditEntry auditEntry;
Promotion prom = promotionRule.getPromotion();

float discount =0;
int fullPriceNights=0 ;
int discountPriceNights =0 ;

try{
transaction = hibernateSession.beginTransaction();
//load the promotion for the given promo_id
PromotionRule rule = (PromotionRule) hibernateSession.get(DefaultPromotionRule.class,new Integer(promotionRule.getID()));

if (prom instanceof DefaultDiscountStayPromotion){
discount = ((DefaultDiscountStayPromotion)prom).getDiscount();
fullPriceNights = ((DefaultDiscountStayPromotion)prom).getFullPriceNights();
discountPriceNights =((DefaultDiscountStayPromotion)prom).getDiscountPriceNights();
}
List promo = hibernateSession.find(FIND_PROMOTION, new Object[]{new Double(discount),new Integer(fullPriceNights),
new Integer(discountPriceNights)},new Type[]{Hibernate.DOUBLE,Hibernate.INTEGER,Hibernate.INTEGER});

//If promotion already exist then get the get the promotion id and set the promotion id for promotion attached to promotion rule and that will make the isPersisted method to return true.
if(promo.size() >0){
for (Iterator i= promo.iterator();i.hasNext();){
Promotion promotion =(Promotion) i.next();
promotionRule.getPromotion().setID(promotion.getID());
rule.setPromotion(promotion);
}
}

if(! promotionRule.getPromotion().isPersisted()){
int promoTypeID = prom.getPromotionType().getID();
int promoLayerID= prom.getPromotionLayer().getID();
int promoTypeLayerID = DEFAULT_INT ;
//List the promo type layer for the given layer attached in the promotion. There always should be an entry in the promotion_type_layer table
// against a promo_layer_id and hard coded promo_type_id.

List promoTypeLayer = hibernateSession.find(FIND_PROMOTION_TYPE_LAYER, new Object[]{new Integer(promoTypeID),
new Integer(promoLayerID)},new Type[]{Hibernate.INTEGER,Hibernate.INTEGER});

//Get the promo_type_layer_id to set into promo

for (Iterator i = promoTypeLayer.iterator();i.hasNext();){
DefaultPromotionTypeLayer typeLayer = (DefaultPromotionTypeLayer) i.next();
promoTypeLayerID = typeLayer.getID();
((DefaultPromotionTypeLayer)(promotionRule.getPromotion())).setID(promoTypeLayerID);
}

hibernateSession.save(promotionRule.getPromotion());
rule.setPromotion(promotionRule.getPromotion());
}
rule.setPromotionRule(promotionRule);
rule.setBookingStartDate(promotionRule.getBookingStartDate());
rule.setBookingEndDate(promotionRule.getBookingEndDate());
rule.setMustStayDays(promotionRule.getMustStayDays());
rule.setPartnerID(promotionRule.getPartnerID());
rule.setActive(promotionRule.isActive());
rule.setPromoEndDate(promotionRule.getPromoEndDate());
rule.setPromoStartDate(promotionRule.getPromoStartDate());
rule.setValidCheckInDays(promotionRule.getValidCheckInDays());
rule.setMinimumStay(promotionRule.getMinimumStay());
rule.setMaximumStay(promotionRule.getMaximumStay());
hibernateSession.update(rule);
auditEntry = new DefaultPromotionRuleAuditEntry(PromotionRuleAuditEntry.UPDATED, promotionRule, user.getId());
hibernateSession.save(auditEntry);
transaction.commit();
}catch(Exception e){
if(transaction !=null){
transaction.rollback();
}
throw new PersistenceException("Unable to update a PromotionRule", e);
}
finally {
closeSession(hibernateSession);
}


}

But I'm wondering if I'm doing it in right way because I'm setting all the attributes the PromotionRule opbject has got :-(.

rule.setPromotionRule(promotionRule);
rule.setBookingStartDate(promotionRule.getBookingStartDate());
rule.setBookingEndDate(promotionRule.getBookingEndDate());
rule.setMustStayDays(promotionRule.getMustStayDays());
rule.setPartnerID(promotionRule.getPartnerID());
rule.setActive(promotionRule.isActive());
rule.setPromoEndDate(promotionRule.getPromoEndDate());
rule.setPromoStartDate(promotionRule.getPromoStartDate());
rule.setValidCheckInDays(promotionRule.getValidCheckInDays());
rule.setMinimumStay(promotionRule.getMinimumStay());
rule.setMaximumStay(promotionRule.getMaximumStay


don't want to use the save /saveOrUpdate method as I knew that every time , I have to update only.

Thanks & Regards,
Irfan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.