In this EJB JPA tutorial, we will learn @OneToOne annotation of the Jave Persistance API. We will also learn the role of @JoinColumn and CascadeType.
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 |
package tube.coding.examples.jpa.entity; import java.io.Serializable; import javax.persistence.*; /** * Entity implementation class for Entity: SavingsAccount * */ @Entity //Sample 3.1: Say the Table Name @Table(name="SavingsAc") public class SavingsAccount implements Serializable { //Sample 3.2: Define fields and annotate to map with db column @Column(name = "Balance") int bal; @Column(name="PersonName") String pName; //Sample 3.3: Map the Primary Key Field @Id @Column(name="PersonID") @GeneratedValue(strategy=GenerationType.IDENTITY) int pid; //Sample 7.04: Define Linking Column @OneToOne(cascade = { CascadeType.ALL }) @JoinColumn(name = "LocId") Location loc; private static final long serialVersionUID = 1L; public SavingsAccount() { super(); } //Sample 3.4: Getters and Setters public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public int getBal() { return bal; } public void setBal(int bal) { this.bal = bal; } public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } //Sample 7.05: Overrdie toString Method public void setLoc(Location loc) { this.loc = loc; } public Location getLoc() { return loc; } //Sample 3.5: Add/Remove Money to/from Savings public int deposit(int credit) { bal = bal + credit; return bal; } public int withdraw(int debit) { bal = bal - debit; return bal; } //Sample 7.06: Overrdie toString Method @Override public String toString() { String ret = ""; ret = pName + "[" + pid + "] - " + bal ; return ret; } } |
Categories: JavaEE-EJB-Tube