cancel
Showing results for 
Search instead for 
Did you mean: 

Is there any way to update an item with a condition?

Former Member
0 Kudos

I would like to update an item only if a condition has been fulfilled(e.g UPDATE WHERE) is something like this doable? I need this for optimistic locking so I would need them to be atomic.

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member224482
Active Contributor
0 Kudos

Here is a pattern usable with only ImpEx and no Java code.

Here is an example using INSERT instead of UPDATE.

 INSERT Language;isocode[unique=true];active
 #% beforeEach: if( FlexibleSearch.getInstance().search("SELECT {pk} FROM {Language} WHERE {isocode} = ?isocode", Collections.singletonMap( "isocode", line.get(1)), de.hybris.platform.jalo.c2l.Language.class).getCount() > 0 ) line.clear() ;
 ;af;false
 ;ar;false

The idea is to use the beforeEach to perform a flexible search and line.clear() to remove the line from being executed. In the example above, the logic is if count > 0 then clear because it already exist (count>0). line.get(1) extract the column #1 value which is the isocode.

Former Member
0 Kudos

This has the same problem for optimistic locking as checking conditions in code.

Former Member
0 Kudos

You could do

 getModelService().lock(yourItem);


     void lock(PK var1);
 
     void lock(Object var1);
 
     <T> T getWithLock(Object var1);


Former Member
0 Kudos

This is pessimistic locking. Thanks anyway!

Former Member
0 Kudos

Hi,

I'm not sure what you're trying to do. But you can do a validate interceptor in order to check your condition

Doc link

 <bean id="productInterceptor" class="...productInterceptor">
 </bean>
 
 <bean id="productInterceptorMapping"
       class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
     <property name="interceptor" ref="productInterceptor"/>
     <property name="typeCode" value="Product"/>
 </bean>

  



 public class ProductInterceptor implements ValidateInterceptor<ProductModel>
 {
 
     @Override
     public void onValidate(ProductModel productModel, InterceptorContext interceptorContext) throws InterceptorException {
 // check on condition
     }
 }
Former Member
0 Kudos

Hello,

I'm trying to do an optimistic locking on an item. How can I use a validator interceptor? From my understanding I would have to query the database for the existing item and compare it to the one that I am trying to save(in the interceptor). How is it guaranteed that between the check and the update no other update is made on the database? Thank you very much!

Former Member
0 Kudos

So yes a validate interceptor is what you want. I edited my answer with a example for a product Item.

Former Member
0 Kudos

Ok but how is it guaranteed that between the check and the update no other update is made on the database on the same entity?

Former Member
0 Kudos

I would say that the Hybris Framework will handle the concurrency. Unless the entity is updated directly in SQL (but that not supposed to happen)

Another way is to put a transaction like

 tx.begin(); 
 boolean success = false;
 try
 {
    // check on condition
    doSomeBusinessLogic();
    success = true;
 }
 finally
 {
    if( success )
       tx.commit();
    else
       tx.rollback();
 }


Former Member
0 Kudos

The only thing I found was related to a field called hjmp that would be activated for all items(and I've read from another thread that it is not recommend), however I need a custom versioning field. Using transactions would not work as there could be a race condition.