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

时间:2018-06-30 12:52:40

标签: hibernate jpa spring-data-jpa

我使用技术jpa,休眠,春季启动-数据,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)!找不到电子钱包类型的属性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;
}


}

实体钱包:

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

2 个答案:

答案 0 :(得分:1)

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

Long createWallet(Long id, String name);

您希望Spring怎样猜测createWallet()方法是用来创建并持久保存带有WalletLong id的{​​{1}}实体的?

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

  

4.4.2. Query Creation

     

该机制会删除前缀createfind…Byread…By,   query…Bycount…By中的方法,然后开始解析   它。

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

  

找不到类型为电子钱包的属性createWallet!

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

JpaRepository

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

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

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

例如:

save()

答案 1 :(得分:0)

尝试按照您的指示做,他向我显示以下错误:

  

org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义的名称为'entityManagerFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:集合没有定义泛型或OneToMany.targetEntity():com.wj.entities.User.wallets

WalletRepository:DAO

public interface WalletRepository extends JpaRepository<Wallet, Long>{

Long save(Long id, String name);

 } 

RestService:

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

    Long id = walletRepository.save(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);
}

实体:用户 我修改了:

@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
@JsonManagedReference
private List<? extends Wallet> wallets = new ArrayList<>();