I m working on a spring project with hibernate-search, here is my code:
spring configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.med.kirana")
@EnableJpaRepositories(basePackages="com.med.kirana.repositories",entityManagerFactoryRef="entityManagerFactory")
@org.springframework.context.annotation.Configuration
public class Configuration extends WebMvcConfigurerAdapter {
...}
here is my repository
public interface ImagesRepository extends JpaRepository<ImageModel, Integer>{
@Query(value="SELECT * FROM IMAGES where product_id = ?1 order by created desc limit 1",nativeQuery=true)
ImageModel findImageByProduct(Integer ProoductId);
}
here is my service layer :
@Service
@Transactional
public class ProductsServiceImpl implements ProductsService{
@Resource
private ProductsRepository productsRepository;
@Resource
private ImagesRepository imagesRepository;
@Override
public ImageModel findImageByProduct(Integer ProoductId) {
ImageModel image=getImagesRepository().findImageByProduct(ProoductId);
if(ObjectUtils.isEmpty(image)){return null;}else{return image;}
}
...
}
and finally, here is the class where i try to autowire the service:
@Component
public class ImageUrlClassBridge implements FieldBridge,ParameterizedBridge {
@Autowired
private ProductsService productsService;
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
ProductModel product=(ProductModel) value;
Logger.getAnonymousLogger().info("\nIndexing product : "+product.getId() );
ImageModel image=getProductsService().findImageByProduct(product.getId());
if(!ObjectUtils.isEmpty(image)){
Logger.getAnonymousLogger().info("\nIMAGE FOUND : "+image.getId() );
Field field = new Field( name,image.getUrl(), luceneOptions.getStore(),
luceneOptions.getIndex(), luceneOptions.getTermVector() );
field.setBoost( luceneOptions.getBoost() );
document.add( field );
}
}
private ProductsService getProductsService() {
return productsService;
}
...
}
Note that I dont have any probleme whit hibernate Search, because autowiring this service in my controllers works fine, here is my controller :
@Controller("products")
public class ProductsManagementPageController {
@Autowired
private ProductsService productsService;
@Autowired
private Indexer indexer;
@Autowired
private Search search;
@ModelAttribute("products")
public List<ProductModel> getAllProducts(Model model) {
return getProductsService().getAllProducts();
}
@RequestMapping(method = RequestMethod.GET, value = "search")
public String search(Model model, @RequestParam String keyword) {
List<ProductModel> products = getSearch().search(keyword);
model.addAttribute("products", products);
return "result";
}
...
}
here is the error I get :
Caused by: java.lang.NullPointerException at com.med.kirana.search.ImageUrlClassBridge.set(ImageUrlClassBridge.java:30)
that means that getProductsService() returns NULL the ImageUrlClassBridge class is called automaticly like an interceptor, called when I call a method that indexes my products in database, so foreach product, the method set (in ImageUrlClassBridge class) is triggered to add and extra field that Product entity doesn't have to index, but that is not the problem, indexing my data is working very well.
here is my Indexer method:
@Transactional
@Repository
public class Indexer {
@PersistenceContext
private EntityManager entityManager;
public void index() throws InterruptedException{
FullTextEntityManager ftem=Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();
}
}
here is my entity :
@Indexed
@ClassBridge(name="Image_url",
impl = ImageUrlClassBridge.class)
@Entity
@Table(name="PRODUCTS")
public class ProductModel {
...//attributes+getters+setters
}
and the index() method is called in my Controller