In this EJB JPA Tutorial, we will complete the example by implementing entity manager bean methods. You will also learn the Transaction commit and Entity states.
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 |
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; } } |
Categories: JavaEE-EJB