我正在使用hibernate-search开发一个spring项目,这是我的代码:
弹簧配置
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.med.kirana")
@EnableJpaRepositories(basePackages="com.med.kirana.repositories",entityManagerFactoryRef="entityManagerFactory")
@org.springframework.context.annotation.Configuration
public class Configuration extends WebMvcConfigurerAdapter {
...}
这是我的存储库
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);
}
这是我的服务层:
@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;}
}
...
}
最后,这是我尝试自动装配服务的类:
@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;
}
...
}
请注意,我没有任何问题惠特hibernate搜索,因为在我的控制器中自动装配此服务工作正常,这是我的控制器:
@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";
}
...
}
这是我得到的错误:
引起:java.lang.NullPointerException 在com.med.kirana.search.ImageUrlClassBridge.set(ImageUrlClassBridge.java:30)
这意味着getProductsService()返回NULL ImageUrlClassBridge类被自动调用就像一个拦截器,当我调用一个在数据库中索引我的产品的方法时调用,所以foreach产品,方法集(在ImageUrlClassBridge类中)被触发添加和产品实体不具有的额外字段索引,但这不是问题,索引我的数据工作得非常好。
这是我的索引方法:
@Transactional
@Repository
public class Indexer {
@PersistenceContext
private EntityManager entityManager;
public void index() throws InterruptedException{
FullTextEntityManager ftem=Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();
}
}
这是我的实体:
@Indexed
@ClassBridge(name="Image_url",
impl = ImageUrlClassBridge.class)
@Entity
@Table(name="PRODUCTS")
public class ProductModel {
...//attributes+getters+setters
}
并在我的Controller中调用index()方法