In this EJB-JPA Tutorial, we will create an entity called SavingsAccount which map to the SavingsAc Table in the SQL Server. We will also learn the Annotations: @Entity, @Table, @Id, @GeneratedValue and @Column.
SavingsAccount.java
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 |
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; 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 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; } } |
Categories: JavaEE-EJB-Tube