...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package sample.jpa.currency; import java.util.List; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; @Stateless @Local public class CurrencyBean implements CurrencyInterface{ @PersistenceContext(unitName="CurrencyRateUnit") private EntityManager em; public Currency createCurrency(String currencyName, String countryName, double rateAgstUSD){ Currency crt1 = em.find(Currency.class, currencyName); if(crt1 != null) throw new IllegalArgumentException ("Currency already exists: Currency Name ("+currencyName+")"); Currency crt = new Currency(); crt.setCountryName(countryName); crt.setCurrencyName(currencyName); crt.setRateAgstUSD(rateAgstUSD); System.out.println("Persisting Currency Rate entity: (Currency = "+currencyName+")"); em.persist(crt); System.out.println("Persisted successfully Currency entity: (Currency = "+currencyName+")"); return crt; } public List listCurrencies(){ if(em == null) System.out.println("em is null!!!"); Query q = em.createQuery("SELECT crt FROM Currency crt"); List currList = q.getResultList(); return currList; } public Currency updateCurrency(String currencyName, double rateAgstUSD){ if(em == null) System.out.println("em is null!!!"); Currency crt1 = em.find(Currency.class, currencyName); if(crt1 == null) throw new IllegalArgumentException ("Currency not found: Currency Name ("+currencyName+")"); crt1.setRateAgstUSD(rateAgstUSD); return crt1; } public void deleteCurrency(String currencyName){ if(em == null) System.out.println("em is null!!!"); Currency crt1 = em.find(Currency.class, currencyName); if(crt1 == null) throw new IllegalArgumentException ("Currency not found: Currency Name ("+currencyName+")"); em.remove(crt1); } public Currency retrieveCurrency(String currencyName){ Currency crt1 = em.find(Currency.class, currencyName); return crt1; } } |
Note |
---|
@PersistenceContext(unitName="CurrencyRateUnit") annotation is used to inject the EntityManager object by container. In this case, the EntityManager object injected is Container Managed EntityManager object. The transaction type is |
11. As outlined above, right click on the META-INF
directory of CurrencyEJB project and create persistence.xml
. Copy the following contents into persistence.xml
.
...
1. Open a browser window and hit the URL http://localhost:8080/console to open admin console. Click on Console Navigation => DB Manager. This will open up DB Viewer and Run SQL portlets on the right side. Enter CurrencyDB in the Create DB textbox and click on the Create button. This will create a database by name CurrencyDB.
...
1. Open a browser window and hit the URL as http://localhost:8080/CurrencyWEB/
This page displays the list of currencies currently in the database. For each currency, the page displays Currency Name, Country Name and Rate Against USD values. It also provides link to Edit and Delete a particular currency.
...