In this EJB-JPA Tutorial, we will learn merge method of the EntityManager. This video implements the fund transfer transaction in the bean layer using the merge method.
Remote Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.Remote; import tube.coding.examples.jpa.entity.SavingsAccount; @Remote public interface SavingAcBeanRemote { //Sample 3.7: Declare the interface method public int newAccount(String accountHolderName, int initialBalance); //Sample 4.00a: Add Methods for Deposit, Withdraw and Account Closure public int depositMoney(int PersonalBankingId, int amount); public int withdrawMoney(int PersonalBankingId, int amount); public boolean closeSavingsAc(int PersonalBankingId); //Sample 5.04: Find Account & return public SavingsAccount findById(int pid); public void TransferFund( SavingsAccount fromAc, SavingsAccount toAc, int amount); } |
Remote Interface Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import tube.coding.examples.jpa.entity.SavingsAccount; @Stateless(mappedName = "SavingAcBean") @LocalBean public class SavingAcBean implements SavingAcBeanRemote { //Sample 3.6: Create Entiry Manager //UnitName is from persistance.xml @PersistenceContext(unitName = "JPASavingAccount") EntityManager em; public SavingAcBean() {} //Sample 3.8: Create New Account public int newAccount(String accountHolderName, int initialBalance) { SavingsAccount account = new SavingsAccount(); account.setBal(initialBalance); account.setpName(accountHolderName); em.persist(account); return account.getPid(); } //Sample 4.00b: Deposit amount to Bank Account @Override public int depositMoney(int PersonalBankingId, int amount) { //Sample 4.20: Find the Account & call deposit on JPA SavingsAccount SavingAc = em.find(SavingsAccount.class, PersonalBankingId); return SavingAc.deposit(amount); } //Sample 4.00c: Withdraw amount from Savings Account public int withdrawMoney(int PersonalBankingId, int amount) { //Sample 4.21: Find the account & Call withdraw on JPA SavingsAccount SavingAc = em.find(SavingsAccount.class, PersonalBankingId); return SavingAc.withdraw(amount); } //Sample 4.00d: Close the account. public boolean closeSavingsAc(int PersonalBankingId) { //Sample 4.22: Find the account and delete the record. Note=> em.remove boolean ret = false; SavingsAccount SavingAc = em.find(SavingsAccount.class, PersonalBankingId); if (SavingAc != null) { em.remove(SavingAc); ret = true; } return ret; } //Sample 5.05: Find account by id and return it @Override public SavingsAccount findById(int pid) { SavingsAccount SavingAc = em.find(SavingsAccount.class,pid); return SavingAc; } //Sample 5.06: Perform Fund Transfer @Override public void TransferFund( SavingsAccount fromAc, SavingsAccount toAc, int amount) { //5.03.1: First Change the From Account SavingsAccount ac1 = em.merge(fromAc); ac1.withdraw(amount); //5.03.2: Now Change the To Account SavingsAccount ac2 = em.merge(toAc); ac2.deposit(amount); } } |
Categories: JavaEE-EJB-Tube