In this EJB-JPA Tutorial, we will learn @ManyToOne and @OneToMany JPA annotations by making use of the Employee & Jobs tables of the pubs database.
Employee Entity
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 |
package tube.coding.examples.jpa.entity; import java.io.Serializable; import java.lang.String; import javax.persistence.*; /** * Entity implementation class for Entity: Employee * */ //Sample 8.01: Entity Mapping @Entity @Table(name = "employee") public class Employee implements Serializable { @Id @Column(name = "emp_id") private String Id; @Column(name = "fname") private String firstName; @Column(name = "minit") private String midName; @Column(name = "lname") private String lastName; @Column(name = "job_lvl") private int jobLevel; //Sample 8.04: Many Employees may Refer one Job Entity @ManyToOne @JoinColumn(name = "job_id") private Job job; private static final long serialVersionUID = 1L; public Employee() { super(); } public String getId() { return this.Id; } public void setId(String Id) { this.Id = Id; } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMidName() { return this.midName; } public void setMidName(String midName) { this.midName = midName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getJobLevel() { return this.jobLevel; } public void setJobLevel(int jobLevel) { this.jobLevel = jobLevel; } //Sample 8.02: Override toString Method @Override public String toString() { return firstName + " " + midName + " " + lastName; } //Sample 8.06: Getter and Setters for Job public Job getJob() { return job; } public void setJob(Job job) { this.job = job; } } |
Job Entity
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 |
package tube.coding.examples.jpa.entity; import java.io.Serializable; import java.lang.String; import java.util.ArrayList; import java.util.List; import javax.persistence.*; import static javax.persistence.CascadeType.ALL; /** * Entity implementation class for Entity: Job * */ @Entity @Table(name = "jobs") public class Job implements Serializable { @Id @Column(name = "job_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int JobId; @Column(name = "job_desc") private String JobDesc; @Column(name = "min_lvl") private int minExp; @Column(name = "max_lvl") private int maxExp; //Sample 8.05: One Job may Refer Many Employees @OneToMany(cascade = ALL, mappedBy = "job") List<Employee> employees = new ArrayList<Employee>(); private static final long serialVersionUID = 1L; public Job() { super(); } public int getJobId() { return this.JobId; } public void setJobId(int JobId) { this.JobId = JobId; } public String getJobDesc() { return this.JobDesc; } public void setJobDesc(String JobDesc) { this.JobDesc = JobDesc; } public int getMinExp() { return this.minExp; } public void setMinExp(int minExp) { this.minExp = minExp; } public int getMaxExp() { return this.maxExp; } public void setMaxExp(int maxExp) { this.maxExp = maxExp; } //Sample 8.07: Getters and Setters for Employee Collection public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } |
Categories: JavaEE-EJB-Tube