java 无法在带有 JpaRepository 的代码中使用 findOne()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49561425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use findOne() in my code with JpaRepository
提问by BMW83
I have a project with class Personne
我有一个类 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();
}
}
and the interface of PersonRepository is here
PersonRepository 的接口在这里
package com.example.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.entities.Personne;
public interface IPersonneRepository extends JpaRepository<Personne, Long> {
}
And in the Controller i add the methode of CRUD (Create, Read, Update, and Delete) those methodes are in this class
在控制器中,我添加了 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;
}
}
}
the application.properties is
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
I tried to change the JpaRepository in CrudRepository but the same problem the same mistake is The method findOne(Example) in the type QueryByExampleExecutor is not applicable for the arguments (Long)i tried to change the method to getOne() but is not perfect I have already used this method before but this time it does not work I do not know why in the same editor. I know very well that I did not yet have the techniques to solve this kind of problem because of the experience that I ask you to show me or due the problem and thank you in advance for the help :)
我试图更改 CrudRepository 中的 JpaRepository 但同样的问题同样的错误是类型 QueryByExampleExecutor 中的方法 findOne(Example) 不适用于参数 (Long)我试图将方法更改为 getOne() 但并不完美我之前已经使用过这种方法,但这次它不起作用我不知道为什么在同一个编辑器中。我非常清楚我还没有解决此类问题的技术,因为我要求您展示给我的经验或由于问题而提前感谢您的帮助:)
回答by kakabali
Try using the following 2 options based on the requirement :-
根据要求尝试使用以下 2 个选项:-
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
There has been some changes with the recent versions of spring data jpa and they have removed the findOne()
method which used work earlier. you can check the post here for reference - https://stackoverflow.com/a/44103020/2600196
最新版本的spring data jpa有了一些变化,他们删除了findOne()
之前使用的工作方法。您可以在此处查看帖子以供参考 - https://stackoverflow.com/a/44103020/2600196
Or revert back to old spring data jpa version
或恢复到旧的 spring 数据 jpa 版本
回答by Christopher
Use findById()
instead of findOne()
.
使用findById()
代替findOne()
。