I have a confusion in how to do update a particular field of all the instances of a bean. I would really appreciate if anybody can throw some light on it.
I have a product table and each product has a catagory and discount% among other fields. What I need to do is change the discount% of all the products of a category. I am thinking of having a home business method in the home interface: public void changeDiscount(String category, double newdiscount);
This method would then fire a query like: update product set discount=? where category=?
My question is: Is this a valid approach? Can I even specify such a query in the deployment descriptor? If I do that, what would happen to the discount values of the beans that are currently in use?
If this is not a valid approach, how should I update the discount value for each product?
The query mentioned by you is doubtful to work in ejb-ql.
Instead try out this approach,
In your Home Interface,write a finder method to find all the products that belong to a particular category.This would return you a Collection of remote/local objects.Then iterate through the Collection and call the setter method for the discount% field.This would update the discount field in all the bean objects.
snippet would look like:
public interface ProductHome extends EJBHome
public Collection findProductsByCategory(String categoryName) throws FinderException
Wouldn't that be extremely slow?
Thanks, Rajat. But I think it will be terribly slow. Is there a quicker way to do this? If not, well...so long EJB.
Even if you are using Entity EJB, you don't have to use them for EVERYTHING. After all, the EJB are just one-of-many possible gateways into your database. Use optimized SQL invoked directly via JDBC for this particular operation, and use Entity EJB for more straightword operations.
If you use BMP,your method is ok ,and you can control the way of connection and execution of SQL.You needn't worry about the problem of out-date data,because every invoktion of EJB's method.
If you use CMP,may be you should consider transformation from CMP to BMP.