【发布时间】:2019-05-31 00:25:39
【问题描述】:
我是 Spring boot 和 JPA 的新手,我创建了一个简单的 JPA 存储库并尝试实现自定义 findBy 方法,但它总是返回 null,即使我在 findBy 之后使用任何虚拟名称,这不是我的类的属性不会显示任何错误。尽管默认的 findBy 方法工作正常。
package com.example.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
// This is my service class.
// Creating employee repository
@Autowired
EmployeeRepo er;
// This is the method I want to implement
public List<Employee> getByname(String name) {
return er.findByName(name);
}
}
//This is my Repository
package com.example.demo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
// Name is a property of Employee class
public List<Employee> findByName(String name);
// Dummy is not a property of Employee class
public List<Employee> findByDummy(String name);
}
它应该给 findByDummy 方法一个错误,但它没有给出。是否将此接口视为通用接口并允许任何声明。 对于 findByName,它返回 null。它应该根据名称进行搜索,对于 findByDummy 它应该显示错误。
提前致谢。
【问题讨论】:
-
@prafull 尝试将
EmployeeRepo注释为@Repository并检查配置类是否使用@EnableJpaRepositories进行注释
标签: spring rest spring-boot spring-data-jpa spring-data