I'm making an app that holds a UserProfile with Wallet that has many Transactions.
Here's the Transaction class:
@Entity
@Table(name = "transactions")
public class Transaction extends BaseEntity {
@Column(name = "amount", nullable = false)
private BigDecimal amount;
@Column(name = "executed_on", nullable = false)
private LocalDateTime executedOn;
@Column(name = "is_top_up")
private boolean isTopUp;
@Column(name = "note")
private String note;
@ManyToOne(targetEntity = UserProfile.class)
private UserProfile sender;
@ManyToOne(targetEntity = UserProfile.class)
private UserProfile receiver;
public Transaction() {
}
Here's the Wallet class
@Entity
@Table(name = "wallets")
public class Wallet extends BaseEntity {
@ManyToMany(targetEntity = Transaction.class, cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
}, fetch = FetchType.EAGER)
@JoinTable(name = "wallets_transactions",
joinColumns = @JoinColumn(name = "wallet_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "transaction_id", referencedColumnName = "id")
)
private Set<Transaction> transactions;
public Wallet() {
this.transactions = new HashSet<>();
}
public Set<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(Set<Transaction> transactions) {
this.transactions = transactions;
}
public void addTransaction(Transaction transaction) {
this.transactions.add(transaction);
}
}
What I want is, to get all transactions by sender and receiver search criteria. For example, user 'A' sent money to user 'B'. I'm using JpaRepository. The end result should be in a Page<Transaction>
class.
So far, when using just findAllBySender(UserProfile sender, Pageable pageable)
, it does work and I get the exact right Transactions. But when I try Page<Transaction> findAllBySenderAndReceiver(UserProfile sender, UserProfile receiver, Pageable pageable);
I get a Page<T>
with 0 elements when my DB has test data with at least 1 record.