java.lang.IllegalArgumentException:无法在JpaRepository中创建查询方法 [英] java.lang.IllegalArgumentException: Failed to create query method in a JpaRepository

查看:336
本文介绍了java.lang.IllegalArgumentException:无法在JpaRepository中创建查询方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用技术jpa,休眠,春季启动-数据,api REST.

I use the technologies 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:无法创建查询方法公共抽象java.lang.Long com.wj.dao.WalletRepository.createWallet(java.lang.Long,java.lang.String)!找不到类型为Wallet的属性createWallet!

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'walletRestService': Unsatisfied dependency expressed through field 'walletRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'walletRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query method public abstract java.lang.Long com.wj.dao.WalletRepository.createWallet(java.lang.Long,java.lang.String)! No property createWallet found for type 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;
}


}

实体钱包:

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


}

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

推荐答案

WalletRepository界面中的此声明对Spring无效:

This declaration in your WalletRepository interface is not valid for Spring :

Long createWallet(Long id, String name);

您如何希望Spring猜测createWallet()方法的目的是创建和持久化具有Long idString nameWallet实体?

How do you want that Spring guesses what the createWallet() method is designed to create and persist a Wallet entity with a Long id and a String name ?

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

In fact the methods you declare in your WalletRepository interface are retrieval methods that rely on naming conventions to allow Spring create the queries for you. And create is not referenced in the Spring Data documentation :

4.4.2.查询创建

该机制去除前缀find…Byread…Byquery…By, 方法中的count…Byget…By,并开始解析其余内容 它.

The mechanism strips the prefixes find…By, read…By, query…By, count…By, and get…By from the method and starts parsing the rest of it.

由于Spring无法识别create,因此它可能尝试将createWallet解析为实体的字段.消息:

As Spring didn't recognize create, it probably tries to resolve createWallet as a field of the entity. Whereas the message :

没有找到类型为Wallet的属性createWallet!

No property createWallet found for type Wallet!

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

To save an entity, instead use the method provided in the JpaRepository :

<S extends T> S save(S entity);

您的存储库的哪个将推断为:

which for your repository will be inferred as :

Wallet save(Wallet entity);

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

And adapt your client code to create the Wallet instance and to pass it to save().

例如:

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

这篇关于java.lang.IllegalArgumentException:无法在JpaRepository中创建查询方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆