In this EJB JPA Tutorial, we will see how to persist OneToOne (1-1) mapped entities into the database.
Remote Interface Change
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 |
package tube.codingexamples.ejb.statelessbean; import java.util.List; import javax.ejb.Remote; import tube.coding.examples.jpa.entity.SavingsAccount; @Remote public interface SavingAcBeanRemote { //Sample 3.7: Declare the interface method //Sample 6.07: Add new Parameters public int newAccount(String accountHolderName, int initialBalance, String Street, String City); //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: Declare method to Search By Id public SavingsAccount queryById(int personalBankingId); //Sample 5.11: Declare method to search by name public SavingsAccount queryByName(String personName); //Sample 5.14: Declare method to List All accounts public List<SavingsAccount> queryAllAccounts(); //Sample 5.20: Declare method to query by balance public List<SavingsAccount> queryByBal(int balance, int mode); } |
Modify 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
package tube.codingexamples.ejb.statelessbean; import java.util.List; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import tube.coding.examples.jpa.entity.Location; 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 = "JPASavingsAccount") EntityManager em; public SavingAcBean() {} //Sample 3.8: Create New Account. [persist API] //Sample 6.08: Make the Change to support new parameter public int newAccount(String accountHolderName, int initialBalance, String Street, String City) { SavingsAccount account = new SavingsAccount(); account.setBal(initialBalance); account.setpName(accountHolderName); //Sample 6.09: Create Category Type Location loc = new Location(); loc.setCity(City); loc.setStreet(Street); account.setLoc(loc); 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. [remove api] 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: Use Entity manager to filter account by id public SavingsAccount queryById(int personalBankingId) { SavingsAccount SavingAc = em.find(SavingsAccount.class, personalBankingId); return SavingAc; } //Sample 5.12: Use JPA QL to filter by account name public SavingsAccount queryByName(String personName) { String JpaSql = "Select sac from SavingsAccount sac " + "where sac.pName = :PersonName"; Query query = em.createQuery(JpaSql); query.setParameter("PersonName", personName); SavingsAccount sa = (SavingsAccount) query.getSingleResult(); return sa; } //Sample 5.15: Use JPA QL to fetch all accounts @SuppressWarnings("unchecked") public List<SavingsAccount> queryAllAccounts() { String JpaSql = "Select sac from SavingsAccount sac"; Query query = em.createQuery(JpaSql); List<SavingsAccount> accounts= query.getResultList(); return accounts; } //Sample 5.21: Use JPA QL to fetch by balance public List<SavingsAccount> queryByBal(int balance, int mode) { //Balance equal String JpaSql = "Select sac from SavingsAccount sac " + "where sac.bal = :Balance"; //Balance less than if (mode == -1) JpaSql = JpaSql.replace('=', '<'); //Balance greater than if (mode == 1) JpaSql = JpaSql.replace('=', '>'); //All set! Get result Query query = em.createQuery(JpaSql); query.setParameter("Balance", balance); List<SavingsAccount> accounts= query.getResultList(); return accounts; } } |
Categories: JavaEE-EJB-Tube