Ich habe ein Repository erweitert CrudRepository
implementiert. Das zugrunde liegende Modell (Account
) hat ein plaidAccountId
Feld und die entsprechende Abfrage-Methode ist findByPlaidAccountId
. Am besten kann ich sagen, ich habe dies richtig verdrahtet, da andere Abfragen auf anderen Modellen korrekt funktionieren. Diese Abfrage gibt jedoch keine Ergebnisse zurück. Ich habe manuell überprüft, dass Elemente in der Datenbank vorhanden sind, die dem angegebenen Abfrageparameter entsprechen, und dennoch wird null zurückgegeben.Spring CrudRepository findBy * Methode gibt keine Ergebnisse zurück
Im Folgenden finden Sie die entsprechenden Konfigurationen. Lass mich wissen, ob es noch etwas gibt, das ich posten könnte. Danke im Voraus.
Die Abfrage Ich Ausführung:
accountService.findByPlaidAccountId(account.getAccountId()
ich angemeldet haben, um sicherzustellen, dass accountService
initialisiert und account.getAccount()
liefert den String-Wert erwartet.
// 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
Update: JPA SQL-Protokollierung
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=?
Kommentare sind nicht für längere Diskussionen; Diese Konversation wurde zum Chat verschoben (http://chat.stackoverflow.com/rooms/161619/discussion-on-question-by-justinraczak-spring-crudrepository-findby-method-retu). –