Skip to content

Commit 9e264cc

Browse files
authored
OneToManyExamples
1 parent 52f25c2 commit 9e264cc

File tree

6 files changed

+285
-0
lines changed

6 files changed

+285
-0
lines changed

com/Address.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Address {
8+
@Id
9+
private int addressId;
10+
private String city;
11+
private String country;
12+
private int zip;
13+
14+
public Address() {
15+
16+
}
17+
public Address(int addressId, String city, String country, int zip) {
18+
super();
19+
this.addressId = addressId;
20+
this.city = city;
21+
this.country = country;
22+
this.zip = zip;
23+
}
24+
public int getAddressId() {
25+
return addressId;
26+
}
27+
public void setAddressId(int addressId) {
28+
this.addressId = addressId;
29+
}
30+
public String getCity() {
31+
return city;
32+
}
33+
public void setCity(String city) {
34+
this.city = city;
35+
}
36+
public String getCountry() {
37+
return country;
38+
}
39+
public void setCountry(String country) {
40+
this.country = country;
41+
}
42+
public int getZip() {
43+
return zip;
44+
}
45+
public void setZip(int zip) {
46+
this.zip = zip;
47+
}
48+
49+
50+
}

com/Employee.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com;
2+
3+
import javax.persistence.CascadeType;
4+
import javax.persistence.Entity;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.Id;
7+
import javax.persistence.JoinColumn;
8+
import javax.persistence.OneToOne;
9+
import javax.persistence.Table;
10+
11+
@Entity
12+
@Table(name="EmpTable")
13+
public class Employee {
14+
@Id
15+
private int empid;
16+
private String name;
17+
private String dept;
18+
private double salary;
19+
@OneToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
20+
@JoinColumn(name="Address_Id",referencedColumnName="addressId")
21+
private Address address;
22+
23+
public Employee() {
24+
25+
}
26+
public Employee(int empid, String name, String dept, double salary, Address address) {
27+
super();
28+
this.empid = empid;
29+
this.name = name;
30+
this.dept = dept;
31+
this.salary = salary;
32+
this.address = address;
33+
}
34+
public int getEmpid() {
35+
return empid;
36+
}
37+
public void setEmpid(int empid) {
38+
this.empid = empid;
39+
}
40+
public String getName() {
41+
return name;
42+
}
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
public String getDept() {
47+
return dept;
48+
}
49+
public void setDept(String dept) {
50+
this.dept = dept;
51+
}
52+
public double getSalary() {
53+
return salary;
54+
}
55+
public void setSalary(double salary) {
56+
this.salary = salary;
57+
}
58+
public Address getAddress() {
59+
return address;
60+
}
61+
public void setAddress(Address address) {
62+
this.address = address;
63+
}
64+
65+
66+
}

com/EmployeeMain.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.EntityTransaction;
6+
import javax.persistence.Persistence;
7+
8+
public class EmployeeMain {
9+
10+
public static void main(String[] args) {
11+
EntityManagerFactory factory=Persistence.createEntityManagerFactory("day3");
12+
EntityManager em=factory.createEntityManager();
13+
EntityTransaction tx=em.getTransaction();
14+
tx.begin();
15+
Address obj=new Address(102, "Bangalore", "India", 110070);
16+
Employee emp=new Employee(112, "Saurabh", "Java", 26000, obj);
17+
//em.persist(obj);
18+
em.persist(emp);
19+
System.out.println("Employee Created.....");
20+
tx.commit();
21+
tx.begin();
22+
/*Employee emp=em.find(Employee.class, 111);
23+
24+
System.out.println("============Employee Details=============");
25+
System.out.println(emp.getName()+"\t"+emp.getSalary()+"\t"+emp.getDept());
26+
System.out.println("============Address Details=============");
27+
System.out.println(emp.getAddress().getAddressId()+"\t"+emp.getAddress().getCity()+"\t"+emp.getAddress().getCountry());
28+
em.remove(emp);
29+
tx.commit();
30+
System.out.println("Employee Deleted...");*/
31+
}
32+
33+
}

oneToMany/Answer.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package oneToMany;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.JoinColumn;
8+
import javax.persistence.ManyToOne;
9+
10+
@Entity
11+
public class Answer {
12+
@Id
13+
@GeneratedValue(strategy=GenerationType.AUTO)
14+
private int aid;
15+
private String answer;
16+
@ManyToOne
17+
@JoinColumn(name="QID", nullable=false)
18+
private Question question;
19+
20+
public Answer() {
21+
22+
}
23+
public Answer(String answer, Question question) {
24+
super();
25+
this.answer = answer;
26+
this.question=question;
27+
}
28+
public int getAid() {
29+
return aid;
30+
}
31+
public void setAid(int aid) {
32+
this.aid = aid;
33+
}
34+
public String getAnswer() {
35+
return answer;
36+
}
37+
public void setAnswer(String answer) {
38+
this.answer = answer;
39+
}
40+
public Question getQuestion() {
41+
return question;
42+
}
43+
public void setQuestion(Question question) {
44+
this.question = question;
45+
}
46+
}

oneToMany/Question.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package oneToMany;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToMany;
11+
12+
@Entity
13+
public class Question {
14+
@Id
15+
@GeneratedValue(strategy=GenerationType.AUTO)
16+
private int qid;
17+
private String question;
18+
private String lavel;
19+
@OneToMany(mappedBy="question",cascade=CascadeType.ALL)
20+
private List<Answer> answer;
21+
public Question() {
22+
23+
}
24+
public Question(String question, String lavel) {
25+
super();
26+
this.question = question;
27+
this.lavel = lavel;
28+
}
29+
public int getQid() {
30+
return qid;
31+
}
32+
public void setQid(int qid) {
33+
this.qid = qid;
34+
}
35+
public String getQuestion() {
36+
return question;
37+
}
38+
public void setQuestion(String question) {
39+
this.question = question;
40+
}
41+
public String getLavel() {
42+
return lavel;
43+
}
44+
public void setLavel(String lavel) {
45+
this.lavel = lavel;
46+
}
47+
public List<Answer> getAnswer() {
48+
return answer;
49+
}
50+
public void setAnswer(List<Answer> answer) {
51+
this.answer = answer;
52+
}
53+
54+
55+
56+
}

oneToMany/QuestionMain.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package oneToMany;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.EntityManagerFactory;
8+
import javax.persistence.EntityTransaction;
9+
import javax.persistence.Persistence;
10+
11+
public class QuestionMain {
12+
13+
public static void main(String[] args) {
14+
EntityManagerFactory factory=Persistence.createEntityManagerFactory("day3");
15+
EntityManager em=factory.createEntityManager();
16+
EntityTransaction tx=em.getTransaction();
17+
tx.begin();
18+
19+
Question q=new Question("What is Java?", "Low");
20+
21+
List<Answer> li=new ArrayList<Answer>();
22+
li.add(new Answer("Java ia OOP.",q));
23+
li.add(new Answer("Java is Simple",q));
24+
li.add(new Answer("Java is Multithreaded",q));
25+
26+
q.setAnswer(li);
27+
28+
em.persist(q);
29+
tx.commit();
30+
System.out.println("=============Done==============");
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)