openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r727639 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed: ContactInfo.java Employee.java JobInfo.java LocationDetails.java ParkingSpot.java PhoneNumber.java ProgramManager.java TestEmbeddable.java
Date Thu, 18 Dec 2008 04:43:33 GMT
Author: faywang
Date: Wed Dec 17 20:43:33 2008
New Revision: 727639

URL: http://svn.apache.org/viewvc?rev=727639&view=rev
Log:
OPENJPA-782: embeddable support. Adding more test cases per 
spec 10.1.7, 10.1.24, 10.1.25, 10.1.34

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ContactInfo.java
Wed Dec 17 20:43:33 2008
@@ -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<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>(); // Bidirectional
+    
+    public List<PhoneNumber> getPhoneNumbers() {
+        return phoneNumbers;
+    }
+    
+    public void addPhoneNumber(PhoneNumber phoneNumber) {
+        phoneNumbers.add(phoneNumber);
+    }
+  
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Employee.java
Wed Dec 17 20:43:33 2008
@@ -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<String> nickNames = new HashSet<String>();
+    
+    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<String> getNickNames() {
+        return nickNames;
+    }
+    
+    public void setNickNames(Set<String> nickNames) {
+        this.nickNames = nickNames;
+    }
+    
+    public void addNickName(String nickName) {
+        nickNames.add(nickName);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/JobInfo.java
Wed Dec 17 20:43:33 2008
@@ -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;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/LocationDetails.java
Wed Dec 17 20:43:33 2008
@@ -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;
+    }
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ParkingSpot.java
Wed Dec 17 20:43:33 2008
@@ -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;
+    }
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/PhoneNumber.java
Wed Dec 17 20:43:33 2008
@@ -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<Employee> employees = new ArrayList<Employee>();
+    
+    public int getNumber() {
+        return number;
+    }
+    
+    public void setNumber(int number) {
+        this.number = number;
+    }
+    
+    public Collection<Employee> getEmployees() {
+        return employees;
+    }
+    
+    public void addEmployees(Employee employee) {
+        employees.add(employee);
+    }
+    
+    
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java?rev=727639&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ProgramManager.java
Wed Dec 17 20:43:33 2008
@@ -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<Employee> manages = new ArrayList<Employee>();
+    
+    public int getId() {
+        return id;
+    }
+    
+    public void setId(int id) {
+        this.id = id;
+    }
+    
+    public Collection<Employee> getManages() {
+        return manages;
+    }
+    
+    public void addManage(Employee e) {
+        manages.add(e);
+    }
+}
+

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java?rev=727639&r1=727638&r2=727639&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
Wed Dec 17 20:43:33 2008
@@ -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<Integer, PhoneNumber> phones = new HashMap<Integer, PhoneNumber>();
+    public Map<Integer, Employee> employees = new HashMap<Integer, Employee>();
+    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 @@
             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 @@
         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 @@
         queryEntityA_Embed_Coll_Embed();
         findEntityA_Embed_Coll_Embed();
     }
+    
+    public void testEmployee() {
+        createEmployeeObj();
+        queryEmployeeObj();
+        findEmployeeObj();
+    }
 
     /*
      * Create EntityA_Coll_String
@@ -393,6 +422,209 @@
     }
 
     /*
+     * 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<ParkingSpot> 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<ProgramManager> 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<PhoneNumber> 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<Employee> es = q.getResultList();
+        for (Employee e : es){
+            assertEmployee(e);
+        }
+        tran.commit();
+        em.close();
+    }
+
+    public void assertProgramManager(ProgramManager pm) {
+        int id = pm.getId();
+        Collection<Employee> 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<PhoneNumber> 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<String> nickNames = e.getNickNames();
+        assertEquals(numNickNames, nickNames.size());
+        
+    }
+    
+    public void assertPhoneNumber(PhoneNumber p) {
+        int number = p.getNumber();
+        Collection<Employee> es = p.getEmployees();
+        assertEquals(numEmployeesPerPhoneNumber, es.size());
+    }
+    
+    public void assertParkingSpot(ParkingSpot p) {
+        String garage = p.getGarage();
+        Employee e = p.getAssignedTo();
+    }
+    
+    /*
      * Find EntityA_Coll_String
      */
     public void findEntityA_Coll_String() {



Mime
View raw message