This is probably a problem that is already solved. But I can't seem to stumble upon the answer. Consider I have 2 beans that are relational. And Corresponding Daos. The trouble is finding all the transactions for a particular account.
Account bean:
public class Account {
private String accountNumber;
private String accountHolder;
// other properties, constructor, getter and setter methods
}
Transaction bean:
public class Transaction {
private String transactionId;
private String accountNumber;
// other properties, constructors, getter and setters.
}
Find all account method in Account Dao:
public class InMemoryAccountDao implements AccountDao {
public static Map<Integer, Account> accountStore = new HashMap<>();
// create, update, delete and find by Id methods.
public Collection<Account> findAllAccounts() {
return accountStore.values();
}
}
Find all transactions for an account in Transaction Dao:
public class InMemoryTransactionDao implements TransactionDao {
public static Map<Integer, Transaction> transactionStore = new HashMap<>();
// create, update, delete and find by Id methods.
public Collection<Transaction> findAllTransactions(Account account) {
Iterator<Transaction> transactionIterator = transactionStore.values().iterator();
Collection<Transaction> transactionsThisAccount = new ArrayList<>();
Transaction tran = null;
while(transactionIterator.hasNext()) {
tran = transactionIterator.next();
if(tran.getAccountNumber() == account.getAccountNumber())
transactionsThisAccount.add(tran);
}
return transactionsThisAccount;
}
}
As you see, I am having to iterate over every transaction and check the account number, with the account number in question. Is there an efficient way of storing and 'querying'? What data structure should I use? If there is a design pattern for relational beans please share me the name/link or any details.
Aucun commentaire:
Enregistrer un commentaire