I was creating a CronJob in Hybris and was facing this Type Mismatch compiler error on line number 44. Tried different things but unable to resolve it.
The error says : " Type mismatch. Cannot convert from SearchResult<Object> to SearchResult<HairProductsModel> "
It could be that this is one of those cases where a method returns an "Object" because it could really return any of a number of types depending on the query. If you know the type that you're expecting you can cast it.
As a fellow Hybris developer, I'm not sure what's going on here, because FlexibleSearchService.search() should support this. Here's the method declaration (from the interface):
So yes, T is a type variable declared in the method, not elsewhere, so basically the compiler should be free to infer any type you declare here. You can say
and the compiler will be happy with that (assuming necessary imports) even if the actual return type is not a SearchResult<Foo> but a SearchResult<Bar> - that will be discovered only at runtime, not compilation time. And the part that errors out will not be this line, but the code that implements it, probably DefaultFlexibleSearchService.convert(SearchResult) and convertRow(Object), which insert casts to T.
At least, that's the way it should work. Is it possible there was another version of this code recently, that used a SearchResult<Object> instead? Is it possible there's still an old class file lying around somewhere? I would do an ant clean all to make sure it's all built fresh. Also, are you using an IDE? IntelliJ with a suitable plugin like Integration for SAP Commerce? That may help if you're not getting a clear view of the classes and methods you're working with. If problems persist, what version of Hybris are you using?