将搜索功能添加到代码交换挑战中

时间:2016-12-14 15:13:36

标签: java spring thymeleaf

我正在参与Java和Spring的代码交换类型挑战,并且我在此购物清单应用的搜索功能中添加了一些问题。在深入研究代码之前,通过搜索功能,我的意思是搜索本地数据库,而不是google。现在我展示代码,然后解释我想要在其下面做什么。

这是错误消息:

2016-12-14 10:00:40.476  WARN 10452 --- [           main] o.h.b.i.SessionFactoryBuilderImpl        : Unrecognized hbm2ddl_auto value : auto.  Supported values include create, create-drop, update, and validate.  Ignoring
2016-12-14 10:00:42.300  INFO 10452 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2016-12-14 10:00:46.009  WARN 10452 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shoppingListController': Unsatisfied dependency expressed through field 'shoppingListRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shoppingListRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property category found for type ShoppingList!
2016-12-14 10:00:46.014  INFO 10452 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2016-12-14 10:00:46.071  INFO 10452 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2016-12-14 10:00:46.393  INFO 10452 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2016-12-14 10:00:46.455 ERROR 10452 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shoppingListController': Unsatisfied dependency expressed through field 'shoppingListRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shoppingListRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property category found for type ShoppingList!

这是我正在编辑的控制器的一部分:

@GetMapping("/lists")
    public String lists(Model model, @RequestParam(name = "srch-term", required = false) String searchTerm) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String email = auth.getName();
        User u = userRepo.findOneByEmail(email);
        if (searchTerm == null || "".equals(searchTerm)) {
            model.addAttribute("lists", shoppingListRepo.findAllByUser(u));
        } else {
            ArrayList<ShoppingList> userLists = new ArrayList<ShoppingList>();
            ArrayList<ShoppingList> lists = shoppingListRepo.findByCategoryContainsOrNameContainsAllIgnoreCase(searchTerm,
                    searchTerm);
            for (ShoppingList list : lists) {
                if (list.getUser() == u) {
                    userLists.add(list);
                }
            }
        model.addAttribute("lists", shoppingListRepo.findAllByUser(u));
        model.addAttribute("user", u);
        }
        return "lists";
    }

这是我编辑前的控制器

@GetMapping("/lists")
    public String lists(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String email = auth.getName();
        User u = userRepo.findOneByEmail(email);
        model.addAttribute("lists", shoppingListRepo.findAllByUser(u));
        model.addAttribute("user", u);
        return "lists";
    }

这是我的存储库

package org.elevenfifty.shoppinglist.repositories;

import java.util.ArrayList;


import org.elevenfifty.shoppinglist.beans.ShoppingList;
import org.elevenfifty.shoppinglist.beans.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ShoppingListRepository extends CrudRepository<ShoppingList, Long> {

    ArrayList<ShoppingList> findAllByUser(User u);

    ArrayList<ShoppingList> findByCategoryContainsOrNameContainsAllIgnoreCase(String categoryPart, String namePart);


}

就像我说的我正在使用搜索功能。我不确定我的错误消息中的属性在哪里我希望一些新鲜的眼睛可以帮助找到我明显遗漏的东西。

1 个答案:

答案 0 :(得分:0)

将类别字段及其getter和setter添加到ShoppingList类。

private String category

public void setCategory(String category){
    this.category = category;
}

public String getCategory(){
    return category;
}