Hibernate java.lang.IllegalArgumentException:未能在JpaRepository中创建查询方法
我使用了jpa、hibernate、spring引导数据、api REST等技术 我有以下错误: org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“walletRestService”的bean时出错:通过字段“walletRepository”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“walletRepository”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:未能创建查询方法public abstract java.lang.Long com.wj.dao.WalletRepository.createWallet(java.lang.Long,java.lang.String)!找不到类型Wallet的属性createWallet 这是我的密码: 实体用户:Hibernate java.lang.IllegalArgumentException:未能在JpaRepository中创建查询方法,hibernate,jpa,spring-data-jpa,Hibernate,Jpa,Spring Data Jpa,我使用了jpa、hibernate、spring引导数据、api REST等技术 我有以下错误: org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“walletRestService”的bean时出错:通过字段“walletRepository”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“wallet
package com.wj.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
public class User implements Serializable{
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
@JsonManagedReference
private List<Wallet> wallets = new ArrayList<>();
public User() {
super();
}
public User(String name) {
super();
this.name = name;
}
public User(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public User(Long id, String name, List<Wallet> wallets) {
super();
this.id = id;
this.name = name;
this.wallets = wallets;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Wallet> getWallets() {
return wallets;
}
public void setWallets(List<Wallet> wallets) {
this.wallets = wallets;
}
}
restApi:
@RequestMapping(value="/wallets", method=RequestMethod.POST)
public Wallet save(@RequestBody Wallet wallet) {
User user = wallet.getUser();
Long id = walletRepository.createWallet(user.getId(), wallet.getName());
User boundUser = wallet.getUser();
User simpleUser = new User(boundUser.getId(), boundUser.getName());
wallet = new Wallet(id, wallet.getName(), simpleUser);
return walletRepository.save(wallet);
}
DAO:
package com.wj.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.wj.entities.Wallet;
public interface WalletRepository extends JpaRepository<Wallet, Long>{
Long createWallet(Long id, String name);
}
@RequestMapping(value=“/wallets”,method=RequestMethod.POST)
公共钱包保存(@RequestBody Wallet){
User User=wallet.getUser();
Long id=walletRepository.createWallet(user.getId(),wallet.getName());
User boundUser=wallet.getUser();
User simpleUser=新用户(boundUser.getId(),boundUser.getName());
wallet=新钱包(id,wallet.getName(),simpleUser);
归还钱包存储。保存(钱包);
}
道:
包com.wj.dao;
导入org.springframework.data.jpa.repository.JpaRepository;
导入com.wj.entities.Wallet;
公共接口WalletRepository扩展了JpaRepository{
长createWallet(长id、字符串名称);
}
您的WalletRepository
界面中的此声明对Spring无效:
Long createWallet(Long id, String name);
您希望Spring如何猜测createWallet()
方法设计用于创建和持久化具有长id
和字符串名称的钱包
实体的内容
事实上,您在WalletRepository
接口中声明的方法是依赖命名约定的检索方法,允许Spring为您创建查询。Spring数据文档中未引用创建的
:
该机制去除前缀find…By
,read…By
,query…By
,
count…By
和get…By
从方法中删除,并开始解析
它
由于Spring无法识别create
,它可能试图将createWallet
解析为实体的一个字段。鉴于信息:
找不到类型Wallet的属性createWallet
要保存实体,请使用JpaRepository
中提供的方法:
<S extends T> S save(S entity);
并调整您的客户端代码以创建钱包
实例并将其传递到保存()
例如:
@RequestMapping(value="/wallets", method=RequestMethod.POST)
public Wallet save(@RequestBody Wallet wallet) {
User boundUser = wallet.getUser();
return walletRepository.save(wallet);
}
展示你的WalletRepository实现。我不太明白你的答案。你能给我举个代码的例子吗?感谢您尝试按照您告诉我的方式操作,他向我显示了以下错误:org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaaautoconfiguration.class]中定义:init方法调用失败;嵌套异常为org.hibernate.AnnotationException:集合既没有泛型类型,也没有定义OneToMany.targetEntity():com.wj.entities.User.wallets您不需要声明该方法。JpaRepository中已经提供了它。但是createWallet()方法的目标是什么?它允许我创建一个钱包,知道钱包有一个用户名和名称。当我在restService中调用createWallet()时,它迫使我在DAO中创建它。否则,我必须在restService中调用什么函数来避免出错,因为我知道要创建钱包,我需要设置user.id和名称?
Wallet save(Wallet entity);
@RequestMapping(value="/wallets", method=RequestMethod.POST)
public Wallet save(@RequestBody Wallet wallet) {
User boundUser = wallet.getUser();
return walletRepository.save(wallet);
}