不能在JpaRepository的代码中使用findOne()

时间:2018-03-29 16:55:26

标签: java spring hibernate spring-boot spring-data-jpa

我有一个班级为Personne的项目

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@SuppressWarnings("serial")
@Entity  
public class Personne implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    @Column(length = 80, name = "NOM")
    private String nom;
    @Column(length = 80, name = "PRENOM")
    private String prenom;
    @Column(length = 80, name = "ADRESSE")
    private String adresse;
    @Column(length = 80, name = "EMAIL")
    private String email;
    @Column(length = 80, name = "TELEPHONE")
    private String telephone;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Personne() {
        super();
    }
}

并且PersonRepository的界面在这里

package com.example.repositories;

import org.springframework.data.jpa.repository.JpaRepository;


import com.example.entities.Personne;

public interface IPersonneRepository extends JpaRepository<Personne, Long> {    

}

在Controller中我添加了CRUD(创建,读取,更新和删除)的方法,这些方法在这个类中

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entities.Personne;
import com.example.repositories.IPersonneRepository;

@RestController
@RequestMapping(value = "/personne")
public class PersonController {

    @Autowired
    private IPersonneRepository iPersonneRepository;

    @PostMapping(value = "/create")
    public Personne createPersonne(@RequestBody Personne personne) {
        return iPersonneRepository.save(personne);
    }

    @PutMapping(value = "/update/{id}")
    public Personne updatePersonne(@PathParam(value = "id") Long id, 
@RequestBody Personne personne) {
        if (id != null) {
            Personne p = iPersonneRepository.findOne(id);
            if (p != null) {
                iPersonneRepository.save(personne);
            }
        }
        return personne;
    }

    @DeleteMapping(value = "/delete/{id}")
    public void delete(@PathParam(value = "id") Long id) {
        if (id != null) {
            Personne p = iPersonneRepository.findOne(id);
            if (p != null) {
                iPersonneRepository.delete(p);
            }
        }
    }

    @GetMapping(value = "/all")
    public List<Personne> allPerson() {
        return iPersonneRepository.findAll();
    }

    @GetMapping(value = "/per/{id}")
    public Personne getOne(@PathParam(value = "id") Long id) {
        if (id != null) {
            Personne p = iPersonneRepository.findOne(id);
            return p;
        } 

    }
}

application.properties是

spring.datasource.url = jdbc:mysql://localhost:3306/spring_boot_DB? 
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.show-sql = true 
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming- 
strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MySQL5Dialect
server.port=8081

我试图更改CrudRepository中的JpaRepository,但同样的问题是同样的错误 QueryByExampleExecutor类型中的方法findOne(Example)不适用于参数(Long) 我试图将方法更改为getOne(),但并不完美 我之前已经使用过这种方法,但这次它不起作用我不知道为什么在同一个编辑器中。我非常清楚我还没有解决这类问题的技巧,因为我要求你给我看看或遇到问题的经验,并提前感谢你的帮助:)

2 个答案:

答案 0 :(得分:2)

根据要求尝试使用以下2个选项: -

onCreateViewHolder()

最新版本的spring数据jpa发生了一些变化,他们删除了之前使用过的yourRespository.findById(id).get();// it will return an Optional instead of null yourRespository.findById(id).orElse(null); // this will return null if you have proper null checks 方法。你可以在这里查看帖子以供参考 - https://stackoverflow.com/a/44103020/2600196

或者恢复旧版弹簧数据jpa版本

答案 1 :(得分:0)

使用findById()代替findOne()