【问题标题】:Adding in a Search feature to a code swap challenge在代码交换挑战中添加搜索功能
【发布时间】:2016-12-14 15:13:36
【问题描述】:

我正在参加 Java 和 Spring 的代码交换类型挑战,我在为此购物清单应用程序添加搜索功能时遇到了一些问题。在我深入代码之前,我所说的搜索功能是指搜索本地数据库,而不是像谷歌这样的东西。现在我将展示代码,然后解释我在这一切下要做什么。

这是错误信息:

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);


}

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

【问题讨论】:

  • 你的ShoppingList实体中没有category这样的字段。
  • 感谢阿卜杜拉!我认为这很简单。

标签: java spring thymeleaf


【解决方案1】:

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

private String category

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

public String getCategory(){
    return category;
}

【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多