import java.util.List; import javax.persistence.EntityManager; public class Main { Address address = new Address(); address.setLine1("test123"); Address address1 = new Address(); address1.setLine1("test456"); University university = new University(); university.setAddress(address1); university.setName("OXF"); College college = new College(); college.setUniversity(university); college.setAddress(address); EntityDAO dao = new EntityDAOImpl(); try { List<College> entity = dao.getEntity(college); for (College a : entity) { System.out.println(">>>>>>>>>>>>>>>>>>>>>>>" + a.getName()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } import java.io.Serializable; import javax.persistence.*; import java.util.Set; @Entity @Table(name="address") @NamedQuery(name="Address.findAll", query="SELECT a FROM Address a") public class Address implements Serializable { private static final long serialVersionUID = 1L; @Id private Integer id; private Integer pin; private String line1; private String line2; private String phone; //bi-directional many-to-one association to College @OneToMany(mappedBy="address") private Set<College> colleges; //bi-directional many-to-one association to University @OneToMany(mappedBy="address") private Set<University> universities; public Address() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Integer getPin() { return this.pin; } public void setPin(Integer pin) { this.pin = pin; } public String getLine1() { return this.line1; } public void setLine1(String line1) { this.line1 = line1; } public String getLine2() { return this.line2; } public void setLine2(String line2) { this.line2 = line2; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public Set<College> getColleges() { return this.colleges; } public void setColleges(Set<College> colleges) { this.colleges = colleges; } public College addCollege(College college) { getColleges().add(college); college.setAddress(this); return college; } public College removeCollege(College college) { getColleges().remove(college); college.setAddress(null); return college; } public Set<University> getUniversities() { return this.universities; } public void setUniversities(Set<University> universities) { this.universities = universities; } public University addUniversity(University university) { getUniversities().add(university); university.setAddress(this); return university; } public University removeUniversity(University university) { getUniversities().remove(university); university.setAddress(null); return university; } } import java.io.Serializable; import javax.persistence.*; import java.util.Set; /** * The persistent class for the university database table. * */ @Entity @Table(name="university") @NamedQuery(name="University.findAll", query="SELECT u FROM University u") public class University implements Serializable { private static final long serialVersionUID = 1L; @Id private Integer id; private String name; //bi-directional many-to-one association to College @OneToMany(mappedBy="university") private Set<College> colleges; //bi-directional many-to-one association to Address @ManyToOne private Address address; public University() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set<College> getColleges() { return this.colleges; } public void setColleges(Set<College> colleges) { this.colleges = colleges; } public College addCollege(College college) { getColleges().add(college); college.setUniversity(this); return college; } public College removeCollege(College college) { getColleges().remove(college); college.setUniversity(null); return college; } public Address getAddress() { return this.address; } public void setAddress(Address address) { this.address = address; } } import java.io.Serializable; import javax.persistence.*; import java.util.Set; /** * The persistent class for the college database table. * */ @Entity @Table(name="college") @NamedQuery(name="College.findAll", query="SELECT c FROM College c") public class College implements Serializable { private static final long serialVersionUID = 1L; @Id private Integer id; private String name; //bi-directional many-to-one association to Address @ManyToOne private Address address; //bi-directional many-to-one association to University @ManyToOne private University university; //bi-directional many-to-one association to Department @OneToMany(mappedBy="college") private Set<Department> departments; public College() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Address getAddress() { return this.address; } public void setAddress(Address address) { this.address = address; } public University getUniversity() { return this.university; } public void setUniversity(University university) { this.university = university; } public Set<Department> getDepartments() { return this.departments; } public void setDepartments(Set<Department> departments) { this.departments = departments; } public Department addDepartment(Department department) { getDepartments().add(department); department.setCollege(this); return department; } public Department removeDepartment(Department department) { getDepartments().remove(department); department.setCollege(null); return department; } } |