spring-data-jpa 为您实现存储库

标签 spring spring-boot spring-data spring-data-jpa

spring-data-jpa 实现存储库

<小时/>

我刚刚开始使用 Spring-bootAccessing-data-jpa,我决定看一下 https://spring.io 提供的指南之一。我决定查看的指南是 accessing-data-jpa 。我遵循它到底并运行了我的应用程序。令我惊讶的是,我收到了一条错误消息。

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property name found for type User!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)

问题所在似乎很简单。它在我的 User 类中寻找属性“name”。但我实际上并没有在任何地方使用属性“名称”。

我的存储库界面:

public interface UserRepository extends JpaRepository<User, Long> {
   List<User> findByName(String userName);
}

我的用户类别:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long userID;
    private LocalDateTime joinDate;
    private String userName;
    private String email;

    protected User(){}

    public User(String userName){
        this.userName = userName;
    }

    @Override
    public String toString() {
        return String.format("User[id=%d, userName=%s, email= %s]",userID, userName,email);
    }
}

应用程序类:

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo(UserRepository repository){
        return (args) -> {
            //Save a few Users
            repository.save(new User("Niek"));
            repository.save(new User("Costas"));
            repository.save(new User("Eric"));
            repository.save(new User("Philip"));
            repository.save(new User("Barry"));

            //Fetch all Users
            log.info("Users Found With findAll()");
            log.info("--------------------------");
            for(User user: repository.findAll()){
                log.info(user.toString());
            }
            log.info("");

            //Fetch one User
            log.info("User found With findOne(1L)");
            log.info("--------------------------");
            log.info(repository.findOne(1L).toString());

            //Fetch one User By Name
            log.info("User found with findByName(\"Niek\")");
            log.info("--------------------------");
            for(User user: repository.findByName("Niek")){
                log.info(user.toString());
            }
        };
    }
}

我想你们中的一些人会想,好吧,你没有实现你的存储库。

In a typical Java application, you’d expect to write a class that implements CustomerRepository. But that’s what makes Spring Data JPA so powerful: You don’t have to write an implementation of the repository interface. Spring Data JPA creates an implementation on the fly when you run the application.

这让我想到了我的问题:

  • 是否是因为 Spring Data JPA 动态提供的存储库实现要求提供属性“name”,而不是仅使用我提供的属性“userName”?
  • 如果上一个问题的答案是"is",是否可以安全地假设提供您自己的实现将大大降低出错的可能性?因为生成的实现似乎符合某种约定。

最佳答案

在写下这个问题时我找到了答案

答案相当简单:

  • 是的,因为 Spring Data JPA 创建的实现无法识别属性“userName”并要求属性“name”。原因很简单,因为我在接口(interface)中声明了该方法。 public List<User> findByName(String userName) ,显然有一个命名约定,其中属性需要适合方法名称。所以我的方法需要是 public List<User> findByUserName(String userName) .
  • 因此,并不是特别推荐您自己实现接口(interface),但是您需要密切注意像这样的小约定,我还没有在任何地方找到这些约定。

关于spring-data-jpa 为您实现存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102385/

相关文章:

java - 捕获异常或者直接抛出业务异常

java - Spring Boot JPA 保存多对多关系

java - 使用编程配置时的 Spring Bean 命名

java - 在 @Query JPA Postgresql 中使用 WITH 子句

Spring Actuator + Kafka Streams - 将 kafka 流状态添加到健康检查端点

运行maven package命令时java spring boot jar文件错误

spring - 为复合 PK 生成的空 ID

Spring 交易: inner transaction changes are "rolled back" by outer transaction (or maybe jpa misuse)

java - 无法执行目标org.apache.maven.plugins :maven-failsafe-plugin:2. 12:验证(验证)

java - Spring 3.0 中的 @ContextConfiguration 给我找不到默认构造函数