package examples.entity.intro; import java.io.Serializable; import javax.persistence.AccessType; import javax.persistence.Entity; import javax.persistence.EntityListener; import javax.persistence.Id; import javax.persistence.NamedQuery; import javax.persistence.PostLoad; import javax.persistence.PostUpdate; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; /** * This demo entity represents a Bank Account. *

* The entity is not a remote object and can only be accessed locally by * clients. However, it is made serializable so that instances can be passed by * value to remote clients for local inspection. *

* Access to persistent state is by direct field access. */ @Entity(access = AccessType.FIELD) @EntityListener(AccountListener.class) @NamedQuery(name="findThem", queryString="SELECT a FROM Account a") public class Account implements Serializable { /** The account number is the primary key for the persistent object */ @Id public int accountNumber; public String ownerName; public int balance; /** * Entity beans must have a public no-arg constructor */ public Account() { // our own primary key generation, workaround for the // time being as Glassfish persistence does not support // auto-generation accountNumber = (int) System.nanoTime(); } /** * Deposit a given amount * @param amount */ public void deposit(int amount) { balance += amount; } /** * Withdraw a given amount, or 0 if it is larger than the balance * @param amount * @return The amount that was withdrawn */ public int withdraw(int amount) { if (amount > balance) { return 0; } else { balance -= amount; return amount; } } public String toString() { return "Acc.# " + accountNumber + ", owner" + ownerName + ", balance: " + balance + " $"; } @PrePersist void prepersist() { System.out.println("pre persist!!"); } @PreUpdate void preupdate() { System.out.println("pre update!!"); } @PostUpdate void postupdate() { System.out.println("post update!!"); } @PostLoad void postload() { System.out.println("post load!!"); } }