Spring CrudRepository findBy *方法返回无结果

时间:2017-12-20 07:23:58

标签: spring hibernate spring-boot spring-data spring-data-jpa

我已经实施了一个扩展CrudRepository的存储库。基础模型(Account)具有plaidAccountId字段,相应的查询方法为findByPlaidAccountId。最好的我可以告诉我,我已正确连线,因为其他模型上的其他查询正常工作。但是,此查询不返回任何结果。我已手动验证数据库中存在的项目是否与提供的查询参数匹配,但仍返回null。

以下是相关配置。如果我能发布其他任何有用的信息,请告诉我。提前谢谢。

我正在执行的查询:

accountService.findByPlaidAccountId(account.getAccountId()

我已经记录以验证accountService是否已初始化,account.getAccount()是否提供了预期的字符串值。

// AccountRepository.java
@Repository
public interface AccountRepository extends CrudRepository<Account, Long> {

    Set<Account> findAllByUser(User user);
    Account findByPlaidAccountId(String plaidAccountId);
    Account findById(int id);
    Account findAccountByPlaidAccountId(String plaidAccountId);
}

-

// AccountService.java
@Service
public class AccountService {

    private AccountRepository accountRepository;

    @Autowired
    public AccountService(AccountRepository repository) {
        this.accountRepository = repository;
    }

    public Account findById(int id) {
        return accountRepository.findById(id);
    }

    public Account findByPlaidAccountId(String plaidAccountId) {
        return accountRepository.findByPlaidAccountId(plaidAccountId);
    }

    public Iterable<Account> findAll() {
        return accountRepository.findAll();
    }

    public Set<Account> findAllByUser(User user) {
        return accountRepository.findAllByUser(user);
    }

    public void saveAccount(Account account) {
        accountRepository.save(account);
    }

    public Account findAccountByPlaidAccountId(String plaidAccountId) {
        return accountRepository.findAccountByPlaidAccountId(plaidAccountId);
    }
}

-

// Account.java
@Entity
@Table(name = "accounts")
public class Account {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "account_id")
private int id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(name = "plaid_account_id")
@NotEmpty(message = "Plaid account number is required")
private String plaidAccountId;

@Column(name = "account_type")
private String accountType;

@Column(name = "account_subtype")
private String accountSubtype;

@Column(name = "institution_id")
private String institutionId;

@Column(name = "current_balance")
private double currentBalance;

@Column(name = "available_balance")
private double availableBalance;

@Column(name = "account_limit")
private double accountLimit;

@Column(name = "name")
private String name;

@Column(name = "official_name")
private String officialName;

@Column(name = "mask")
private String mask;

@ManyToOne
@JoinColumn(name = "plaid_item_id", nullable = false)
private PlaidItem plaidItem;

public Account() {
}
// ... getters and setters

更新:JPA SQL日志记录

Hibernate: select account0_.account_id as account_1_0_, account0_.account_limit as account_2_0_, account0_.account_subtype as account_3_0_, account0_.account_type as account_4_0_, account0_.available_balance as availabl5_0_, account0_.current_balance as current_6_0_, account0_.institution_id as institut7_0_, account0_.mask as mask8_0_, account0_.name as name9_0_, account0_.official_name as officia10_0_, account0_.plaid_account_id as plaid_a11_0_, account0_.plaid_item_id as plaid_i12_0_, account0_.user_id as user_id13_0_ from accounts account0_ where account0_.plaid_account_id=?

3 个答案:

答案 0 :(得分:3)

我在你的代码中看到了一些可能产生未来问题的事情,我不知道他们是否会给你带来麻烦,但将来会给你一个。

// AccountRepository.java 
 @Repository 
 public interface AccountRepository extends CrudRepository<Account, Long> {        
    Set<Account> findAllByUser(User user);        
    Account findByPlaidAccountId(String plaidAccountId);      
    Account findById(int id);        
    Account findByPlaidAccountId(String plaidAccountId); 
 }

首先是&#34; CrudRepository<Account, Long>&#34;你在这里很长,但是你的班级的ID是&#34; int&#34;,它们应该是相同的。

Account findByPlaidAccountId(String plaidAccountId);
Account findAccountByPlaidAccountId(String plaidAccountId);

这两种方法期望返回一个Account对象,但是plaidAccountId不是唯一的或pk,所以你必须要一个列表。 如果您确定只有一个元素并且没有人处理您的应用程序中的列表,只需将存储库更改为Set/List<Account>,服务保持这样:

public Account findByPlaidAccountId(String plaidAccountId) {
        return accountRepository.findByPlaidAccountId(plaidAccountId).get(0);
}

findFirstByPlaidAccountId

祝零回报好运。)。

答案 1 :(得分:1)

出于某种原因,我的值已保存到数据库,包括其引号,因此正确的查询在技术上"<value>"

答案 2 :(得分:0)

如果您按MyComponent.hello()搜索(如何调用您的方法),我想您要传递该ID而不是6

plaidAccountId