-->
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.  [ 6 posts ] 
Author Message
 Post subject: Backup old object before update
PostPosted: Thu Jan 12, 2006 8:55 am 
Newbie

Joined: Thu Jan 12, 2006 8:24 am
Posts: 3
Hi All :)

I have a question about updating objects.
In the application some objects require some special management before update, for example when i have to update my Product i have to store also a ProductBck object, that stores some info about Product and other stuff (an externale TicketId etc.).
The application use DAOs.

The ProductService class pseudo-code is like-this:

Code:
class ProductService {
void updateProduct(int ticketId, Product product) {
    Product storedProduct = ProductDAO.load();
   
    ProductBck backup = new ProductBck();
    ProductBck.setImportantInfo(storedProduct.getImportantInfo());
    ProductBck.setTicketId(tickeId);

    ProductBckDAO.create(productBck);

    ProductDAO.update(product);

}
}


while the client code is similar to this:

Code:
Product product = ProductDAO.load();
product.setImportantInfo(infoObj);

ProductService productService = new ProductService();
productService.update(product);


The DAOs was based on plain JDBC, but now we have replaced all with Hibernate.
But with Hibernate when we can't use this code because when we load the product from the DB in the ProductService we doesn't load the product actually on DB but the product already modified by the client code.

There is some workaround to avoid this? There is a good solution that doesn't change the ProductService interface?

Thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:12 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Hibernate Provides Listeners and Interceptors that let you plug in code before it does a save/update/flush/delete etc.
Refer to
http://www.hibernate.org/hib_docs/v3/re ... terceptors
for Interceptors.
In case you are using Hibernate 3.x, you can write an implementation of SaveOrUpdateListener and do your backup operation in that. Stub code would be
Code:
public class MySaveOrUpdateListener implements SaveOrUpdateListener
{
    public Serializable onSaveOrUpdate(SaveOrUpdateEvent event)
    {
         Product prod = (Product) event.getObject();
         // Whatever u want to do with PRoduct and ProductDAO
        return <id of the product>; // Not sure if anything needs to be returned as the code doesnt seem to be using it ?
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:42 am 
Newbie

Joined: Thu Jan 12, 2006 8:24 am
Posts: 3
rajasaur wrote:
Hibernate Provides Listeners and Interceptors that let you plug in code before it does a save/update/flush/delete etc.


Thanks for reply :)
I tried Interceptors and they work fine but what about the "ticketId" in the:

Code:
void updateProduct(int ticketId, Product product)


The listener that have to backup the data doesn't know about this, there is no way to pass it to the listener, or i'm wrong?
This ticketId is an external id (provided by the client code) and have to be saved with the ProductBck.

(sorry for the Product storedProduct = ProductDAO.load();,
it is "ProductDAO.load(productId);", i write some lines on the fly, no cut&paste)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:11 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Sorry, cant think of another way. Maybe you can have a method in your Product(thats not mapped to any column in the product table) and fill it in before you send it to saveOrUpdate. Since the Product object is available in the Listener, you can get it from there.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:25 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Seems to me that the new one should be a new object that has some of the original values copied in - what you're suggesting is trying to work with 2 DB records using the same object which feels wrong....

Thus perhaps you want to do something like:
Code:
Product orig = ProductDAO.load("...");
Product new = new Product();
// copy the properties
BeanUtils.copyProperties(orig, new);

// set anything on the orig or new
orig.setTicket(...);
new.setImportantShit(...);
ProductDAO.update(orig);
ProductDAO.create(new);


Just an idea...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:32 pm 
Newbie

Joined: Thu Jan 12, 2006 8:24 am
Posts: 3
Quote:
timrobertson100 wrote:
Seems to me that the new one should be a new object that has some of the original values copied in - what you're suggesting is trying to work with 2 DB records using the same object which feels wrong....


Not exactly, i'm trying to work with 2 different objects and 2 different records, a Product and a ProductBck, the problem is that there is not possible retrieve the original Product values, this thing was possible when i was using DAO implementented with plain JDBC.
Your solution solve the problem but break the interface, the client code of ProductService have to ignore this details.

Quote:
rajasaur wrote:
Sorry, cant think of another way. Maybe you can have a method in your Product(thats not mapped to any column in the product table) and fill it in before you send it to saveOrUpdate. Since the Product object is available in the Listener, you can get it from there.


It is a bit tricky but maybe an idea :)

Thanks for replies!


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