java.lang.IllegalArgumentException:无法在 JpaRepository 中创建查询方法

woo*_*son 2 hibernate jpa spring-data-jpa

我使用 jpa、hibernate、spring boot - data、api REST 技术。

我有以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“walletRestService”的bean时出错:通过字段“walletRepository”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“walletRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Failed to create query method public abstract java.lang.Long com.wj.dao.WalletRepository.createWallet(java.lang.Long,java.lang.String)!找不到类型 Wallet 的属性 createWallet!

这是我的代码:

实体用户:

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;
}


}
Run Code Online (Sandbox Code Playgroud)

实体钱包:

@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name="user_id")
@JsonBackReference    
private User user;

public Wallet() {
    super();
}




public Wallet(String name, User user) {
    super();
    this.name = name;
    this.user = user;
}




public Wallet(Long id, String name, User user) {
    super();
    this.id = id;
    this.name = name;
    this.user = user;
}



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 User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}


}
Run Code Online (Sandbox Code Playgroud)

休息API:

@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);
}
Run Code Online (Sandbox Code Playgroud)

dav*_*xxx 6

WalletRepository 接口中的此声明对 Spring 无效:

Long createWallet(Long id, String name);
Run Code Online (Sandbox Code Playgroud)

您希望 Spring 如何猜测该createWallet()方法旨在创建和持久化Wallet具有 aLong id和 a的实体String name

实际上,您在WalletRepository接口中声明的方法是检索方法,它们依赖于命名约定以允许 Spring 为您创建查询。并且create未在 Spring Data 文档中引用:

4.4.2. 查询创建

该机制从方法中去除前缀find…Byread…Byquery…Bycount…Byget…By并开始解析它的其余部分。

由于 Spring 无法识别create,它可能会尝试解析createWallet为实体的一个字段。而消息:

找不到类型 Wallet 的属性 createWallet!

要保存实体,请改用 中提供的方法JpaRepository

<S extends T> S save(S entity);
Run Code Online (Sandbox Code Playgroud)

对于您的存储库,这将被推断为:

Wallet save(Wallet entity);
Run Code Online (Sandbox Code Playgroud)

并调整您的客户端代码以创建Wallet实例并将其传递给save().

如 :

@RequestMapping(value="/wallets", method=RequestMethod.POST)
public Wallet save(@RequestBody Wallet wallet) {
    User boundUser = wallet.getUser();
    return walletRepository.save(wallet);
}
Run Code Online (Sandbox Code Playgroud)