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
This commit is contained in:
Fay Wang 2008-12-18 04:43:33 +00:00
parent e63fbd5226
commit 5256a6c516
8 changed files with 493 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 @@ 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<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
*/