Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
JAVA
JAVA
borderStylesolid
titleCurrencyBean.java
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 JTA and the persistence scope is Transaction.
Since we have not declared any transactional attributes in the CurrencyBean, the default is Container Managed Transaction with transaction attribute being REQUIRED for the bean methods. So, the EntityManager object will join the EJB transaction whenever a method is called on the bean.

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.

...