From 5256a6c516638034fe42f83d1e1ce153918c0b3f Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Thu, 18 Dec 2008 04:43:33 +0000 Subject: [PATCH] OPENJPA-782: embeddable support. Adding more test cases per spec 10.1.7, 10.1.24, 10.1.25, 10.1.34 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@727639 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/embed/ContactInfo.java | 25 ++ .../openjpa/persistence/embed/Employee.java | 70 ++++++ .../openjpa/persistence/embed/JobInfo.java | 29 +++ .../persistence/embed/LocationDetails.java | 28 +++ .../persistence/embed/ParkingSpot.java | 39 +++ .../persistence/embed/PhoneNumber.java | 35 +++ .../persistence/embed/ProgramManager.java | 33 +++ .../persistence/embed/TestEmbeddable.java | 236 +++++++++++++++++- 8 files changed, 493 insertions(+), 2 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java new file mode 100644 index 000000000..9d07ebc48 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java @@ -0,0 +1,25 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; +import java.util.*; + +@Embeddable +public class ContactInfo { + @ManyToMany + @JoinTable(name="EMP_PHONE", + joinColumns = @JoinColumn(name="Emp_ID", referencedColumnName="EmpId"), + inverseJoinColumns= + @JoinColumn(name="PHONE_ID", referencedColumnName="Number") + + ) + List phoneNumbers = new ArrayList(); // Bidirectional + + public List getPhoneNumbers() { + return phoneNumbers; + } + + public void addPhoneNumber(PhoneNumber phoneNumber) { + phoneNumbers.add(phoneNumber); + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java new file mode 100644 index 000000000..320dce583 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java @@ -0,0 +1,70 @@ +package org.apache.openjpa.persistence.embed; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.*; + +@Entity +@Table(name="EmpEmbedTest") +public class Employee { + @Id + int empId; + + @Embedded + ContactInfo contactInfo; + + @Embedded + JobInfo jobInfo; + + @Embedded + LocationDetails location; + + @ElementCollection // use default table (PERSON_NICKNAMES) + @Column(name="name", length=50) + protected Set nickNames = new HashSet(); + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public void setContactInfo(ContactInfo contactInfo) { + this.contactInfo = contactInfo; + } + + public ContactInfo getContactInfo() { + return contactInfo; + } + + public void setJobInfo(JobInfo jobInfo) { + this.jobInfo = jobInfo; + } + + public JobInfo getJobInfo() { + return jobInfo; + } + + public LocationDetails getLocationDetails() { + return location; + } + + public void setLocationDetails(LocationDetails location) { + this.location = location; + } + + public Set getNickNames() { + return nickNames; + } + + public void setNickNames(Set nickNames) { + this.nickNames = nickNames; + } + + public void addNickName(String nickName) { + nickNames.add(nickName); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java new file mode 100644 index 000000000..ab34d5012 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java @@ -0,0 +1,29 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; +import java.util.*; + +@Embeddable +public class JobInfo { + + String jobDescription; + + @ManyToOne + ProgramManager pm; // Bidirectional + + public void setJobDescription(String jobDescription) { + this.jobDescription = jobDescription; + } + + public String getJobDescription() { + return jobDescription; + } + + public void setProgramManager(ProgramManager pm) { + this.pm = pm; + } + + public ProgramManager getProgramManager() { + return pm; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java new file mode 100644 index 000000000..07fef625e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java @@ -0,0 +1,28 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; + +@Embeddable +public class LocationDetails { + int officeNumber; + + @OneToOne + ParkingSpot parkingSpot; + + public int getOfficeNumber() { + return officeNumber; + } + + public void setOfficeNumber(int officeNumber) { + this.officeNumber = officeNumber; + } + + public ParkingSpot getParkingSpot() { + return parkingSpot; + } + + public void setParkingSpot(ParkingSpot parkingSpot) { + this.parkingSpot = parkingSpot; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java new file mode 100644 index 000000000..d1f0afe3f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java @@ -0,0 +1,39 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; + +@Entity +@Table(name="ParkingEmbedTest") + +public class ParkingSpot { + @Id int id; + String garage; + + @OneToOne(mappedBy="location.parkingSpot") + Employee assignedTo; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getGarage() { + return garage; + } + + public void setGarage(String garage) { + this.garage = garage; + } + + public Employee getAssignedTo() { + return assignedTo; + } + + public void setAssignedTo(Employee assignedTo) { + this.assignedTo = assignedTo; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java new file mode 100644 index 000000000..883d54c09 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java @@ -0,0 +1,35 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; + +import java.util.*; + +@Entity +@Table(name="PhoneEmbedTest") + +public class PhoneNumber { + @Id + int number; + + @ManyToMany(mappedBy="contactInfo.phoneNumbers") + Collection employees = new ArrayList(); + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + public Collection getEmployees() { + return employees; + } + + public void addEmployees(Employee employee) { + employees.add(employee); + } + + + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java new file mode 100644 index 000000000..39bc12e39 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java @@ -0,0 +1,33 @@ +package org.apache.openjpa.persistence.embed; + +import javax.persistence.*; + +import java.util.*; + +@Entity +@Table(name="PMEmbedTest") + +public class ProgramManager { + @Id + int id; + + @OneToMany(mappedBy="jobInfo.pm") + Collection manages = new ArrayList(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Collection getManages() { + return manages; + } + + public void addManage(Employee e) { + manages.add(e); + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java index 2e7fa81e7..c4042a4c3 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java @@ -18,20 +18,41 @@ */ package org.apache.openjpa.persistence.embed; +import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Query; + import org.apache.openjpa.persistence.test.SingleEMFTestCase; + public class TestEmbeddable extends SingleEMFTestCase { public int numEmbeddables = 1; public int numBasicTypes = 1; public int ID = 1; + public int numProgramManagers = 2; + public int numNickNames = 3; + + public int numEmployeesPerPhoneNumber = 1; + public int numPhoneNumbersPerEmployee = 2; + public int numEmployeesPerProgramManager = 2; + public int numEmployees = numProgramManagers * numEmployeesPerProgramManager; + public int numPhoneNumbers = numEmployees * numPhoneNumbersPerEmployee; + + public Map phones = new HashMap(); + public Map employees = new HashMap(); + public int empId = 1; + public int phoneId = 1; + public int pmId = 1; + public int parkingSpotId = 1; public void setUp() { setUp(Embed.class, Embed_Coll_Embed.class, Embed_Coll_Integer.class, @@ -41,7 +62,9 @@ public class TestEmbeddable extends SingleEMFTestCase { EntityA_Embed_Coll_Integer.class, EntityA_Embed_Embed.class, EntityA_Embed_Embed_ToMany.class, EntityA_Embed_ToMany.class, EntityA_Embed_ToOne.class, EntityB1.class, - EntityA_Coll_Embed_Embed.class, + EntityA_Coll_Embed_Embed.class, ContactInfo.class, + Employee.class, JobInfo.class, LocationDetails.class, + ParkingSpot.class, PhoneNumber.class, ProgramManager.class, CLEAR_TABLES); } @@ -87,7 +110,7 @@ public class TestEmbeddable extends SingleEMFTestCase { findEntityA_Embed_Embed(); } - public void atestEntityA_Coll_Embed_Embed() { + public void testEntityA_Coll_Embed_Embed() { createEntityA_Coll_Embed_Embed(); queryEntityA_Coll_Embed_Embed(); findEntityA_Coll_Embed_Embed(); @@ -98,6 +121,12 @@ public class TestEmbeddable extends SingleEMFTestCase { queryEntityA_Embed_Coll_Embed(); findEntityA_Embed_Coll_Embed(); } + + public void testEmployee() { + createEmployeeObj(); + queryEmployeeObj(); + findEmployeeObj(); + } /* * Create EntityA_Coll_String @@ -392,6 +421,209 @@ public class TestEmbeddable extends SingleEMFTestCase { return embed; } + /* + * Create Employee + */ + public void createEmployeeObj() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + createPhoneNumbers(em); + createEmployees(em); + createProgramManagers(em); + + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createProgramManagers(EntityManager em) { + empId = 1; + for (int i = 0; i < numProgramManagers; i++) + createProgramManager(em, pmId++); + } + + public void createProgramManager(EntityManager em, int id) { + ProgramManager pm = new ProgramManager(); + pm.setId(id); + for (int i = 0; i < numEmployeesPerProgramManager; i++) { + Employee e = employees.get(empId++); + pm.addManage(e); + JobInfo jobInfo = new JobInfo(); + jobInfo.setJobDescription("jobDescription" + e.getEmpId()); + jobInfo.setProgramManager(pm); + e.setJobInfo(jobInfo); + } + em.persist(pm); + } + + public void createEmployees(EntityManager em) { + phoneId = 1; + for (int i = 0; i < numEmployees; i++) { + Employee e = createEmployee(em, empId++); + employees.put(e.getEmpId(), e); + } + } + + public Employee createEmployee(EntityManager em, int id) { + Employee e = new Employee(); + e.setEmpId(id); + ContactInfo contactInfo = new ContactInfo(); + for (int i = 0; i < numPhoneNumbersPerEmployee; i++) { + PhoneNumber phoneNumber = phones.get(phoneId++); + contactInfo.addPhoneNumber(phoneNumber); + e.setContactInfo(contactInfo); + phoneNumber.addEmployees(e); + em.persist(phoneNumber); + } + ParkingSpot parkingSpot = createParkingSpot(em, parkingSpotId++); + LocationDetails location = new LocationDetails(); + location.setOfficeNumber(id); + location.setParkingSpot(parkingSpot); + e.setLocationDetails(location); + parkingSpot.setAssignedTo(e); + for (int i = 0; i < numNickNames; i++) + e.addNickName("nickName" + id + i); + em.persist(parkingSpot); + em.persist(e); + return e; + } + + public void createPhoneNumbers(EntityManager em) { + for (int i = 0; i < numPhoneNumbers; i++) { + PhoneNumber p = new PhoneNumber(); + p.setNumber(phoneId++); + phones.put(p.getNumber(), p); + em.persist(p); + } + } + + public ParkingSpot createParkingSpot(EntityManager em, int id) { + ParkingSpot p = new ParkingSpot(); + p.setId(id); + p.setGarage("garage" + id); + em.persist(p); + return p; + } + + public void findEmployeeObj() { + EntityManager em = emf.createEntityManager(); + ProgramManager pm = em.find(ProgramManager.class, 1); + assertProgramManager(pm); + + pm = em.find(ProgramManager.class, 2); + assertProgramManager(pm); + + Employee e = em.find(Employee.class, 1); + assertEmployee(e); + + PhoneNumber p = em.find(PhoneNumber.class, 1); + assertPhoneNumber(p); + + ParkingSpot ps = em.find(ParkingSpot.class, 1); + assertParkingSpot(ps); + + em.close(); + } + + public void queryEmployeeObj() { + queryProgramManager(emf); + queryEmployee(emf); + queryPhoneNumber(emf); + queryParkingSpot(emf); + } + + public void queryParkingSpot(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select p from ParkingSpot p"); + List ps = q.getResultList(); + for (ParkingSpot p : ps){ + assertParkingSpot(p); + } + tran.commit(); + em.close(); + } + + public void queryProgramManager(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select pm from ProgramManager pm"); + List pms = q.getResultList(); + for (ProgramManager pm : pms){ + assertProgramManager(pm); + } + tran.commit(); + em.close(); + } + + public void queryPhoneNumber(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select p from PhoneNumber p"); + List ps = q.getResultList(); + for (PhoneNumber p : ps){ + assertPhoneNumber(p); + } + tran.commit(); + em.close(); + } + + public void queryEmployee(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select e from Employee e"); + List es = q.getResultList(); + for (Employee e : es){ + assertEmployee(e); + } + tran.commit(); + em.close(); + } + + public void assertProgramManager(ProgramManager pm) { + int id = pm.getId(); + Collection es = pm.getManages(); + assertEquals(numEmployeesPerProgramManager, es.size()); + for (Employee e : es) { + assertEmployee(e); + } + } + + public void assertEmployee(Employee e) { + int id = e.getEmpId(); + ContactInfo c = e.getContactInfo(); + List phones = c.getPhoneNumbers(); + assertEquals(numPhoneNumbersPerEmployee, phones.size()); + for (PhoneNumber p : phones) { + assertPhoneNumber(p); + } + + LocationDetails loc = e.getLocationDetails(); + int officeNumber = loc.getOfficeNumber(); + ParkingSpot p = loc.getParkingSpot(); + assertParkingSpot(p); + ProgramManager pm = e.getJobInfo().getProgramManager(); + Set nickNames = e.getNickNames(); + assertEquals(numNickNames, nickNames.size()); + + } + + public void assertPhoneNumber(PhoneNumber p) { + int number = p.getNumber(); + Collection es = p.getEmployees(); + assertEquals(numEmployeesPerPhoneNumber, es.size()); + } + + public void assertParkingSpot(ParkingSpot p) { + String garage = p.getGarage(); + Employee e = p.getAssignedTo(); + } + /* * Find EntityA_Coll_String */