...
8. Copy the following contents into Account.java
.
Code Block | |||||||||
---|---|---|---|---|---|---|---|---|---|
| |||||||||
package sample.jpa; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.PostLoad; import javax.persistence.PostUpdate; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Table; @Entity @Table(name = "ACCOUNTBME") public class Account implements Serializable { @Id public int accountNumber; public int personId; public double balance; public Account() { } public int getAccountNumber() { return accountNumber; } public void setAccountNumber(int accountNumber) { this.accountNumber = accountNumber; } public int getPersonId() { return personId; } public void setPersonId(int personId) { this.personId = personId; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } |
9. Similarly, as given in the previous step, create Person.java
and add the following contents to it.
Code Block | |||||||||
---|---|---|---|---|---|---|---|---|---|
| |||||||||
package sample.jpa; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "PERSONBME") public class Person implements Serializable { @Id public int personId; public String personName; public String address; public Person() { } public int getPersonId() { return personId; } public void setPersonId(int personId) { this.personId = personId; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public String getAddress() { return address; } public void setAddress(String address) { System.out.println("Address="+address); this.address = address; } } |
9. Similarly, create AccountInterface.java
and copy the following contents.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package sample.jpa; import java.util.Collection; public interface AccountInterface { public void initialize(int accountNumber); public String getPersonName(); public void setPersonName(String personName); public String getAddress(); public void setAddress(String address); public double getBalance(); public void setBalance(double balance); public void updateAllValues(String address, String personName, double balance); public void withdraw(double amount); public void deposit(double amount); } |
10. Similarly, create AccountBean.java.java
and copy the following contents. Note that several lines have been truncated for display.
Code Block | |||||||||
---|---|---|---|---|---|---|---|---|---|
| |||||||||
package sample.jpa; import javax.annotation.PostConstruct; import javax.ejb.Local; import javax.ejb.Remove; import javax.ejb.Stateful; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; @Stateful @TransactionManagement(TransactionManagementType.CONTAINER) @Local(AccountInterface.class) public class AccountBean implements AccountInterface{ @PersistenceUnit(unitName="AccountUnit") private EntityManagerFactory emf; private EntityManager em; Account account; Person person; @PostConstruct public void init(){ em = emf.createEntityManager(); } public void initialize(int accountNumber){ account = em.find(Account.class, accountNumber); if(account == null)throw new IllegalStateException ("Account number ("+accountNumber+") not found"); person = em.find(Person.class, account.getPersonId()); if(person == null)throw new IllegalStateException("Person Id("+account.getPersonId()+") not found. There is a descripancy in the database for account number ("+accountNumber+")"); } public String getPersonName(){ return person.getPersonName(); } public void setPersonName(String personName){ person.setPersonName(personName); } public String getAddress(){ return person.getAddress(); } public void setAddress(String address){ person.setAddress(address); } public void updateAllValues(String address, String personName, double balance){ em.joinTransaction(); setAddress(address); setPersonName(personName); setBalance(balance); } public double getBalance(){ return account.getBalance(); } public void setBalance(double balance){ account.setBalance(balance); } public void withdraw(double amount){ if(amount > getBalance())throw new IllegalStateException ("The amount("+amount+") to be withdrawn is more than the available balance("+getBalance()+")"); em.joinTransaction(); setBalance(getBalance() - amount); } public void deposit(double amount){ em.joinTransaction(); setBalance(getBalance() + amount); } @Remove public void destroy(){ em.close(); } } |
...
1. Start the geronimo server and open the admin console on a browser window with the url
http://localhost:8080/console.
2. Click on the Embedded DB => DB Manager on the Console Navigation portlet.
...
1. Open a browser window and hit the URL as http://localhost:8080/BeanManagedJPA-WEB/. This brings upthe screen shot below. Click on the View Account Details link.
...