get models by example in Hybris

Sometimes, we are required to fetch data from our custom Item type in Hybris. So first thing comes to our mind is to write flexible search query. But you really don't need to do that if you know Hybris OOTB has getModelByExample and getModelsByExample API of flexibleSearchService, using which we can easily get the desired results.


Scenario

Find CustomProduct having the same name

Solution

You can create a method in DAO class. In which you need to create example model by setting required field (e.g name). Now call getModelsByExample method of the flexibleSearchService with exampleModel.
 public List<CustomProductModel> findProductHavingSameName(final String name)
 {

     CustomProductModel exampleModel = new CustomProductModel();
     exampleModel.setName(name);
     return getFlexibleSearchService().getModelsByExample(exampleModel);
 }

Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 15+ years experience in SAP Commerce Cloud (Hybris), SAP Spartacus. Extensive experience in SAP Hybris development, third-party integrations, project architecture and design... Read more

Comments

  1. Other than Equals qualifier, can we use other parameters here as well, like starts with, greater than etc?

    ReplyDelete
  2. For models by example, flexibleSearchService only supports tow methods getModelByExample and getModelsByExample. So you can't use other parameters here.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hybris flexible search query examples

How to Install temporary Hybris license?

How to remove or update all data records in Hybris?