添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I use the technologies jpa, hibernate, spring boot - data, api REST.

I have the following error:

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!

Here is my code :

Entity user:

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

Entity wallet:

@GeneratedValue private Long id; private String name; @ManyToOne @JoinColumn(name="user_id") @JsonBackReference private User user; public Wallet() { super();
@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);
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);

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

Long createWallet(Long id, String name);

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 ?

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. Query Creation

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

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

No property createWallet found for type Wallet!

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

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

Such as :

@RequestMapping(value="/wallets", method=RequestMethod.POST)
public Wallet save(@RequestBody Wallet wallet) {
    User boundUser = wallet.getUser();
    return walletRepository.save(wallet);
                I did not really understand your answer. Could you give me an example with my code? thank you
– woodson
                Jun 30, 2018 at 13:30
                Trying to do as you told me, he shows me the following error:  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.wj.entities.User.wallets
– woodson
                Jun 30, 2018 at 13:43
                You don't need to declare the method. It is already provided in the JpaRepository. But what is the goal of the createWallet() method ?
– davidxxx
                Jun 30, 2018 at 16:01
                It allows me to create a wallet, knowing that a wallet has a user and name. When I call createWallet () in restService it forces me to create it in the DAO. Otherwise what function must I call in restService, to avoid making an error, knowing that to create a wallet I need to set the user.id and the name?
– woodson
                Jul 1, 2018 at 10:50
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.