From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../src/examples/entity/META-INF/application.xml | 11 +++ .../src/examples/entity/META-INF/persistence.xml | 13 ++++ .../examples/entity/bid/many_to_many/Course.java | 48 +++++++++++++ .../examples/entity/bid/many_to_many/Student.java | 50 +++++++++++++ .../bid/many_to_many/StudentCourseBidBean.java | 53 ++++++++++++++ .../many_to_many/client/StudentCourseClient.java | 41 +++++++++++ .../bid/many_to_many/interfaces/StudentCourse.java | 14 ++++ .../examples/entity/bid/one_to_many/Company.java | 48 +++++++++++++ .../bid/one_to_many/CompanyEmployeeInfo.java | 29 ++++++++ .../bid/one_to_many/CompanyEmployeeOMBidBean.java | 81 +++++++++++++++++++++ .../examples/entity/bid/one_to_many/Employee.java | 53 ++++++++++++++ .../one_to_many/client/CompanyEmployeeClient.java | 44 ++++++++++++ .../one_to_many/interfaces/CompanyEmployeeOM.java | 16 +++++ .../src/examples/entity/bid/one_to_one/Order.java | 45 ++++++++++++ .../bid/one_to_one/OrderShipmentBidBean.java | 40 +++++++++++ .../examples/entity/bid/one_to_one/Shipment.java | 53 ++++++++++++++ .../bid/one_to_one/client/OrderShipmentClient.java | 39 ++++++++++ .../bid/one_to_one/interfaces/OrderShipment.java | 14 ++++ .../Micah Examples/src/examples/entity/build.xml | 83 ++++++++++++++++++++++ .../src/examples/entity/joined/Car.java | 15 ++++ .../src/examples/entity/joined/Coupe.java | 25 +++++++ .../src/examples/entity/joined/Motorcycle.java | 16 +++++ .../src/examples/entity/joined/RoadVehicle.java | 67 +++++++++++++++++ .../joined/RoadVehicleStatelessJoinedBean.java | 46 ++++++++++++ .../src/examples/entity/joined/Roadster.java | 29 ++++++++ .../entity/joined/client/RoadVehicleClient.java | 28 ++++++++ .../interfaces/RoadVehicleStatelessJoined.java | 12 ++++ .../src/examples/entity/single_table/Car.java | 21 ++++++ .../src/examples/entity/single_table/Coupe.java | 31 ++++++++ .../examples/entity/single_table/Motorcycle.java | 22 ++++++ .../examples/entity/single_table/RoadVehicle.java | 72 +++++++++++++++++++ .../single_table/RoadVehicleStatelessBean.java | 50 +++++++++++++ .../src/examples/entity/single_table/Roadster.java | 31 ++++++++ .../single_table/client/RoadVehicleClient.java | 54 ++++++++++++++ .../interfaces/RoadVehicleStateless.java | 16 +++++ .../examples/entity/uni/many_to_many/Course.java | 44 ++++++++++++ .../examples/entity/uni/many_to_many/Student.java | 50 +++++++++++++ .../uni/many_to_many/StudentCourseUniBean.java | 48 +++++++++++++ .../many_to_many/client/StudentCourseClient.java | 30 ++++++++ .../uni/many_to_many/interfaces/StudentCourse.java | 14 ++++ .../entity/uni/many_to_one/BusinessAddress.java | 42 +++++++++++ .../examples/entity/uni/many_to_one/Employee.java | 45 ++++++++++++ .../uni/many_to_one/EmployeeAddressMOUniBean.java | 42 +++++++++++ .../many_to_one/client/EmployeeAddressClient.java | 28 ++++++++ .../interfaces/EmployeeAddressMOUni.java | 12 ++++ .../examples/entity/uni/one_to_many/Company.java | 47 ++++++++++++ .../uni/one_to_many/CompanyEmployeeOMUniBean.java | 73 +++++++++++++++++++ .../examples/entity/uni/one_to_many/Employee.java | 42 +++++++++++ .../one_to_many/client/CompanyEmployeeClient.java | 35 +++++++++ .../one_to_many/interfaces/CompanyEmployeeOM.java | 16 +++++ .../src/examples/entity/uni/one_to_one/Order.java | 45 ++++++++++++ .../uni/one_to_one/OrderShipmentUniBean.java | 33 +++++++++ .../examples/entity/uni/one_to_one/Shipment.java | 42 +++++++++++ .../uni/one_to_one/client/OrderShipmentClient.java | 30 ++++++++ .../uni/one_to_one/interfaces/OrderShipment.java | 12 ++++ 55 files changed, 2040 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/application.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/persistence.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Course.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Student.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/StudentCourseBidBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/client/StudentCourseClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/interfaces/StudentCourse.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Company.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeInfo.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeOMBidBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Employee.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/client/CompanyEmployeeClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/interfaces/CompanyEmployeeOM.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Order.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/OrderShipmentBidBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Shipment.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/client/OrderShipmentClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/interfaces/OrderShipment.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/build.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Car.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Coupe.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Motorcycle.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicle.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicleStatelessJoinedBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Roadster.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/client/RoadVehicleClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/interfaces/RoadVehicleStatelessJoined.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Car.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Coupe.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Motorcycle.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicle.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicleStatelessBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Roadster.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/client/RoadVehicleClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/interfaces/RoadVehicleStateless.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Course.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Student.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/StudentCourseUniBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/client/StudentCourseClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/interfaces/StudentCourse.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/BusinessAddress.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/Employee.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/EmployeeAddressMOUniBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/client/EmployeeAddressClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/interfaces/EmployeeAddressMOUni.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Company.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/CompanyEmployeeOMUniBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Employee.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/client/CompanyEmployeeClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/interfaces/CompanyEmployeeOM.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Order.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/OrderShipmentUniBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Shipment.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/client/OrderShipmentClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/interfaces/OrderShipment.java (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity') diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/application.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/application.xml new file mode 100644 index 0000000..68de436 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/application.xml @@ -0,0 +1,11 @@ + + + EntityExamples + + EntityExamplesEjb.jar + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/persistence.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/persistence.xml new file mode 100644 index 0000000..e5be93a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + jdbc/__default + + + + + + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Course.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Course.java new file mode 100644 index 0000000..2d6a18c --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Course.java @@ -0,0 +1,48 @@ +package examples.entity.bid.many_to_many; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +@Entity(name="CourseBid") +public class Course implements Serializable { + private int id; + private String courseName; + private Collection students = new ArrayList(); + + public Course() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + @ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="courses") + public Collection getStudents() { + return students; + } + + public void setStudents(Collection students) { + this.students = students; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Student.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Student.java new file mode 100644 index 0000000..fc40746 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/Student.java @@ -0,0 +1,50 @@ +package examples.entity.bid.many_to_many; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + +@Entity(name="StudentBid") +public class Student implements Serializable { + private int id; + private String name; + private Collection courses = new ArrayList(); + + public Student() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER) + @JoinTable(name="STUDENTBID_COURSEBID") + public Collection getCourses() { + return courses; + } + + public void setCourses(Collection courses) { + this.courses = courses; + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/StudentCourseBidBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/StudentCourseBidBean.java new file mode 100644 index 0000000..919f9ce --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/StudentCourseBidBean.java @@ -0,0 +1,53 @@ +package examples.entity.bid.many_to_many; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.bid.many_to_many.interfaces.StudentCourse; + +@Stateless +public class StudentCourseBidBean implements StudentCourse { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Course c1 = new Course(); + c1.setCourseName("EJB 3.0 101"); + + Course c2 = new Course(); + c2.setCourseName("EJB 3.0 202"); + + Student s1 = new Student(); + s1.setName("Micah"); + + s1.getCourses().add(c1); + + c1.getStudents().add(s1); + + Student s2 = new Student(); + s2.setName("Tes"); + + s2.getCourses().add(c1); + s2.getCourses().add(c2); + + c1.getStudents().add(s2); + c2.getStudents().add(s2); + + em.persist(s1); + em.persist(s2); + } + + public List getAllStudents() { + Query q = em.createQuery("SELECT s FROM StudentBid s"); + return q.getResultList(); + } + + public List getAllCourses() { + Query q = em.createQuery("SELECT c FROM CourseBid c"); + return q.getResultList(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/client/StudentCourseClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/client/StudentCourseClient.java new file mode 100644 index 0000000..4537ad1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/client/StudentCourseClient.java @@ -0,0 +1,41 @@ +package examples.entity.bid.many_to_many.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.bid.many_to_many.Course; +import examples.entity.bid.many_to_many.Student; +import examples.entity.bid.many_to_many.interfaces.StudentCourse; + + +public class StudentCourseClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + StudentCourse sc = (StudentCourse)ic.lookup(StudentCourse.class.getName()); + + sc.doSomeStuff(); + + for (Object o : sc.getAllStudents()) { + Student s = (Student)o; + System.out.println("Student: "+s.getName()); + for (Object o1 : s.getCourses()) { + Course c = (Course)o1; + System.out.println("\tCourse: "+c.getCourseName()); + } + } + System.out.println(); + for (Object o : sc.getAllCourses()) { + Course c = (Course)o; + System.out.println("Course: "+c.getCourseName()); + for (Object o1 : c.getStudents()) { + Student s = (Student)o1; + System.out.println("\tStudent: "+s.getName()); + } + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/interfaces/StudentCourse.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/interfaces/StudentCourse.java new file mode 100644 index 0000000..90328e4 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/many_to_many/interfaces/StudentCourse.java @@ -0,0 +1,14 @@ +package examples.entity.bid.many_to_many.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface StudentCourse { + public void doSomeStuff(); + + public List getAllStudents(); + + public List getAllCourses(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Company.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Company.java new file mode 100644 index 0000000..0183e18 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Company.java @@ -0,0 +1,48 @@ +package examples.entity.bid.one_to_many; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity(name="CompanyOMBid") +public class Company implements Serializable { + private int id; + private String name; + private Collection employees = new ArrayList(); + + public Company() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="company") + public Collection getEmployees() { + return employees; + } + + public void setEmployees(Collection employees) { + this.employees = employees; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeInfo.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeInfo.java new file mode 100644 index 0000000..02fbbb7 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeInfo.java @@ -0,0 +1,29 @@ +package examples.entity.bid.one_to_many; + +import java.io.Serializable; + +public class CompanyEmployeeInfo implements Serializable { + private String cName; + private String eName; + + public CompanyEmployeeInfo(String c, String e) { + cName = c; + eName = e; + } + + public String getCName() { + return cName; + } + + public void setCName(String name) { + cName = name; + } + + public String getEName() { + return eName; + } + + public void setEName(String name) { + eName = name; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeOMBidBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeOMBidBean.java new file mode 100644 index 0000000..77b763a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/CompanyEmployeeOMBidBean.java @@ -0,0 +1,81 @@ +package examples.entity.bid.one_to_many; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.bid.one_to_many.interfaces.CompanyEmployeeOM; + +@Stateless +public class CompanyEmployeeOMBidBean implements CompanyEmployeeOM { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Company c = new Company(); + c.setName("M*Power Internet Services, Inc."); + + //Collection employees = new ArrayList(); + Employee e = new Employee(); + e.setName("Micah Silverman"); + e.setSex('M'); + e.setCompany(c); + //employees.add(e); + c.getEmployees().add(e); + + e = new Employee(); + e.setName("Tes Silverman"); + e.setSex('F'); + e.setCompany(c); + //employees.add(e); + c.getEmployees().add(e); + + //c.setEmployees(employees); + em.persist(c); + + c = new Company(); + c.setName("Sun Microsystems"); + + //employees = new ArrayList(); + e = new Employee(); + e.setName("Rima Patel"); + e.setSex('F'); + e.setCompany(c); + //employees.add(e); + c.getEmployees().add(e); + + e = new Employee(); + e.setName("James Gosling"); + e.setSex('M'); + e.setCompany(c); + //employees.add(e); + c.getEmployees().add(e); + + //c.setEmployees(employees); + em.persist(c); + + c = new Company(); + c.setName("Bob's Bait & Tackle"); + em.persist(c); + } + + public List getCompanies() { + Query q = em.createQuery("SELECT c FROM CompanyOMBid c"); + return q.getResultList(); + } + + public List getEmployees() { + Query q = em.createQuery("SELECT e FROM EmployeeOMBid e"); + return q.getResultList(); + } + + public void deleteAll() { + Query q = em.createQuery("DELETE FROM EmployeeOMBid"); + q.executeUpdate(); + q = em.createQuery("DELETE FROM CompanyOMBid"); + q.executeUpdate(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Employee.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Employee.java new file mode 100644 index 0000000..c0e85ec --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/Employee.java @@ -0,0 +1,53 @@ +package examples.entity.bid.one_to_many; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity(name="EmployeeOMBid") +public class Employee implements Serializable { + private int id; + private String name; + private char sex; + private Company company; + + public Employee() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public char getSex() { + return sex; + } + + public void setSex(char sex) { + this.sex = sex; + } + + @ManyToOne + public Company getCompany() { + return company; + } + + public void setCompany(Company company) { + this.company = company; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/client/CompanyEmployeeClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/client/CompanyEmployeeClient.java new file mode 100644 index 0000000..8959f37 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/client/CompanyEmployeeClient.java @@ -0,0 +1,44 @@ +package examples.entity.bid.one_to_many.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.bid.one_to_many.Company; +import examples.entity.bid.one_to_many.Employee; +import examples.entity.bid.one_to_many.interfaces.CompanyEmployeeOM; + + +public class CompanyEmployeeClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + CompanyEmployeeOM ceom = (CompanyEmployeeOM)ic.lookup(CompanyEmployeeOM.class.getName()); + + ceom.deleteAll(); + + ceom.doSomeStuff(); + + for (Object o : ceom.getCompanies()) { + Company c = (Company)o; + System.out.println("Here are the employees for company: "+c.getName()); + for (Employee e : c.getEmployees()) { + System.out.println("\tName: "+e.getName()+", Sex: "+e.getSex()); + } + System.out.println(); + } + System.out.println(); + + for (Object o : ceom.getEmployees()) { + Employee e = (Employee)o; + Company c = e.getCompany(); + + System.out.println("Employee: "+e.getName()+" works for: "+c.getName()); + + System.out.println(); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/interfaces/CompanyEmployeeOM.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/interfaces/CompanyEmployeeOM.java new file mode 100644 index 0000000..e79f59f --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_many/interfaces/CompanyEmployeeOM.java @@ -0,0 +1,16 @@ +package examples.entity.bid.one_to_many.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface CompanyEmployeeOM { + public void doSomeStuff(); + + public List getCompanies(); + + public List getEmployees(); + + public void deleteAll(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Order.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Order.java new file mode 100644 index 0000000..6af54aa --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Order.java @@ -0,0 +1,45 @@ +package examples.entity.bid.one_to_one; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity(name="OrderBid") +public class Order implements Serializable { + private int id; + private String orderName; + private Shipment shipment; + + public Order() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getOrderName() { + return orderName; + } + + public void setOrderName(String orderName) { + this.orderName = orderName; + } + + @OneToOne(cascade={CascadeType.PERSIST}) + public Shipment getShipment() { + return shipment; + } + + public void setShipment(Shipment shipment) { + this.shipment = shipment; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/OrderShipmentBidBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/OrderShipmentBidBean.java new file mode 100644 index 0000000..792c2de --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/OrderShipmentBidBean.java @@ -0,0 +1,40 @@ +package examples.entity.bid.one_to_one; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.bid.one_to_one.interfaces.OrderShipment; + +@Stateless +public class OrderShipmentBidBean implements OrderShipment { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Shipment s = new Shipment(); + s.setCity("Austin"); + s.setZipcode("78727"); + + Order o = new Order(); + o.setOrderName("Software Order"); + o.setShipment(s); + + s.setOrder(o); + + em.persist(o); + } + + public List getOrders() { + Query q = em.createQuery("SELECT o FROM OrderBid o"); + return q.getResultList(); + } + + public List getShipments() { + Query q = em.createQuery("SELECT s FROM ShipmentBid s"); + return q.getResultList(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Shipment.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Shipment.java new file mode 100644 index 0000000..c778ae2 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/Shipment.java @@ -0,0 +1,53 @@ +package examples.entity.bid.one_to_one; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity(name="ShipmentBid") +public class Shipment implements Serializable { + private int id; + private String city; + private String zipcode; + private Order order; + + public Shipment() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + + @OneToOne(mappedBy="shipment") + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/client/OrderShipmentClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/client/OrderShipmentClient.java new file mode 100644 index 0000000..d99444c --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/client/OrderShipmentClient.java @@ -0,0 +1,39 @@ +package examples.entity.bid.one_to_one.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.bid.one_to_one.Order; +import examples.entity.bid.one_to_one.Shipment; +import examples.entity.bid.one_to_one.interfaces.OrderShipment; + + +public class OrderShipmentClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + OrderShipment os = (OrderShipment)ic.lookup(OrderShipment.class.getName()); + + os.doSomeStuff(); + + System.out.println("Bidirectional One-To-One client\n"); + + for (Object o : os.getOrders()) { + Order order = (Order)o; + System.out.println("Order "+order.getId()+": "+order.getOrderName()); + System.out.println("\tShipment details: "+order.getShipment().getCity()+" "+order.getShipment().getZipcode()); + } + + System.out.println(); + + for (Object o : os.getShipments()) { + Shipment shipment = (Shipment)o; + System.out.println("Shipment: "+shipment.getCity()+" "+shipment.getZipcode()); + System.out.println("\tOrder details: "+shipment.getOrder().getOrderName()); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/interfaces/OrderShipment.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/interfaces/OrderShipment.java new file mode 100644 index 0000000..cc4ae2b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/bid/one_to_one/interfaces/OrderShipment.java @@ -0,0 +1,14 @@ +package examples.entity.bid.one_to_one.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface OrderShipment { + public void doSomeStuff(); + + public List getOrders(); + + public List getShipments(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/build.xml new file mode 100644 index 0000000..f73d142 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/build.xml @@ -0,0 +1,83 @@ + + ]> + + + + + + + + + + + + &include; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Car.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Car.java new file mode 100644 index 0000000..38cbda0 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Car.java @@ -0,0 +1,15 @@ +package examples.entity.joined; + +import java.io.Serializable; + +import javax.persistence.Entity; + +@Entity(name="CarJoined") +public class Car extends RoadVehicle implements Serializable { + public final AcceleratorType acceleratorType = AcceleratorType.PEDAL; + + public Car() { + super(); + numWheels = 4; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Coupe.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Coupe.java new file mode 100644 index 0000000..59df47b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Coupe.java @@ -0,0 +1,25 @@ +package examples.entity.joined; + +import java.io.Serializable; + +import javax.persistence.Entity; + +@Entity(name="CoupeJoined") +public class Coupe extends Car implements Serializable { + public enum BoringFactor {BORING,BORINGER,BORINGEST}; + + private BoringFactor boringFactor; + + public Coupe() { + super(); + numPassengers = 5; + } + + public BoringFactor getBoringFactor() { + return boringFactor; + } + + public void setBoringFactor(BoringFactor boringFactor) { + this.boringFactor = boringFactor; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Motorcycle.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Motorcycle.java new file mode 100644 index 0000000..14c83ec --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Motorcycle.java @@ -0,0 +1,16 @@ +package examples.entity.joined; + +import java.io.Serializable; + +import javax.persistence.Entity; + +@Entity(name="MotorcycleJoined") +public class Motorcycle extends RoadVehicle implements Serializable { + public final AcceleratorType acceleratorType = AcceleratorType.THROTTLE; + + public Motorcycle() { + super(); + numWheels = 2; + numPassengers = 2; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicle.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicle.java new file mode 100644 index 0000000..9fe8c3e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicle.java @@ -0,0 +1,67 @@ +package examples.entity.joined; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +@Entity(name="RoadVehicleJoined") +@Table(name="RoadVehicleJoined") +@Inheritance(strategy=InheritanceType.JOINED) +public class RoadVehicle implements Serializable { + public enum AcceleratorType {PEDAL,THROTTLE}; + + @Id + protected int id; + protected int numPassengers; + protected int numWheels; + protected String make; + protected String model; + + public RoadVehicle() { + id = (int) System.nanoTime(); + } + + public int getNumPassengers() { + return numPassengers; + } + + public void setNumPassengers(int numPassengers) { + this.numPassengers = numPassengers; + } + + public int getNumWheels() { + return numWheels; + } + + public void setNumWheels(int numWheels) { + this.numWheels = numWheels; + } + + public int getId() { + return id; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String toString() { + return "Make: "+make+", Model: "+model+", Number of passengers: "+numPassengers; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicleStatelessJoinedBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicleStatelessJoinedBean.java new file mode 100644 index 0000000..9b46869 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/RoadVehicleStatelessJoinedBean.java @@ -0,0 +1,46 @@ +package examples.entity.joined; + +import java.util.ArrayList; +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.joined.Coupe.BoringFactor; +import examples.entity.joined.Roadster.CoolFactor; +import examples.entity.joined.interfaces.RoadVehicleStatelessJoined; + +@Stateless +public class RoadVehicleStatelessJoinedBean implements RoadVehicleStatelessJoined { + @PersistenceContext(unitName="pu1") + EntityManager em; + + public void doSomeStuff() { + Coupe c = new Coupe(); + c.setMake("Bob"); + c.setModel("E400"); + c.setBoringFactor(BoringFactor.BORING); + em.persist(c); + + Roadster r = new Roadster(); + r.setMake("Mini"); + r.setModel("Cooper S"); + r.setCoolFactor(CoolFactor.COOLEST); + em.persist(r); + + Motorcycle m = new Motorcycle(); + em.persist(m); + } + + public List getAllCars() { + Query q = em.createQuery("SELECT c.id FROM CarJoined c"); + List ids = q.getResultList(); + ArrayList al = new ArrayList(); + for (Object o : ids) { + al.add(em.find(Car.class,(Integer)o)); + } + return al; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Roadster.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Roadster.java new file mode 100644 index 0000000..b93dbf5 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/Roadster.java @@ -0,0 +1,29 @@ +package examples.entity.joined; + +import java.io.Serializable; + +import javax.persistence.Entity; + +@Entity(name="RoadsterJoined") +public class Roadster extends Car implements Serializable { + public enum CoolFactor {COOL,COOLER,COOLEST}; + + private CoolFactor coolFactor; + + public Roadster() { + super(); + numPassengers = 2; + } + + public CoolFactor getCoolFactor() { + return coolFactor; + } + + public void setCoolFactor(CoolFactor coolFactor) { + this.coolFactor = coolFactor; + } + + public String toString() { + return super.toString()+", CoolFactor: "+coolFactor; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/client/RoadVehicleClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/client/RoadVehicleClient.java new file mode 100644 index 0000000..64cf21e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/client/RoadVehicleClient.java @@ -0,0 +1,28 @@ +package examples.entity.joined.client; + +import java.util.List; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.joined.interfaces.RoadVehicleStatelessJoined; + + +public class RoadVehicleClient { + public static void main(String[] args) { + InitialContext ic; + try { + ic = new InitialContext(); + RoadVehicleStatelessJoined rvs = (RoadVehicleStatelessJoined)ic.lookup(RoadVehicleStatelessJoined.class.getName()); + rvs.doSomeStuff(); + + List l = rvs.getAllCars(); + for (Object o : l) { + System.out.println("Car: "+o); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/interfaces/RoadVehicleStatelessJoined.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/interfaces/RoadVehicleStatelessJoined.java new file mode 100644 index 0000000..b205c01 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/joined/interfaces/RoadVehicleStatelessJoined.java @@ -0,0 +1,12 @@ +package examples.entity.joined.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface RoadVehicleStatelessJoined { + public void doSomeStuff(); + + public List getAllCars(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Car.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Car.java new file mode 100644 index 0000000..5c72d7f --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Car.java @@ -0,0 +1,21 @@ +package examples.entity.single_table; + +import java.io.Serializable; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity(name="CarSingle") +@DiscriminatorValue("CAR") +public class Car extends RoadVehicle implements Serializable { + public final AcceleratorType acceleratorType = AcceleratorType.PEDAL; + + public Car() { + super(); + numWheels = 4; + } + + public String toString() { + return "Car: "+super.toString(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Coupe.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Coupe.java new file mode 100644 index 0000000..31af7d5 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Coupe.java @@ -0,0 +1,31 @@ +package examples.entity.single_table; + +import java.io.Serializable; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity(name="CoupeSingle") +@DiscriminatorValue("COUPE") +public class Coupe extends Car implements Serializable { + public enum BoringFactor {BORING,BORINGER,BORINGEST}; + + private BoringFactor boringFactor; + + public Coupe() { + super(); + numPassengers = 5; + } + + public BoringFactor getBoringFactor() { + return boringFactor; + } + + public void setBoringFactor(BoringFactor boringFactor) { + this.boringFactor = boringFactor; + } + + public String toString() { + return "Coupe: "+super.toString(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Motorcycle.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Motorcycle.java new file mode 100644 index 0000000..018cb5d --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Motorcycle.java @@ -0,0 +1,22 @@ +package examples.entity.single_table; + +import java.io.Serializable; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity(name="MotorcycleSingle") +@DiscriminatorValue("MOTORCYCLE") +public class Motorcycle extends RoadVehicle implements Serializable { + public final AcceleratorType acceleratorType = AcceleratorType.THROTTLE; + + public Motorcycle() { + super(); + numWheels = 2; + numPassengers = 2; + } + + public String toString() { + return "Motorcycle: "+super.toString(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicle.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicle.java new file mode 100644 index 0000000..75a258a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicle.java @@ -0,0 +1,72 @@ +package examples.entity.single_table; + +import java.io.Serializable; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +@Entity(name="RoadVehicleSingle") +@Table(name="RoadVehicleSingle") +@Inheritance(strategy=InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name="DISC", discriminatorType=DiscriminatorType.STRING) +@DiscriminatorValue("ROADVEHICLE") +public class RoadVehicle implements Serializable { + public enum AcceleratorType {PEDAL,THROTTLE}; + + @Id + protected int id; + protected int numPassengers; + protected int numWheels; + protected String make; + protected String model; + + public RoadVehicle() { + id = (int) System.nanoTime(); + } + + public int getNumPassengers() { + return numPassengers; + } + + public void setNumPassengers(int numPassengers) { + this.numPassengers = numPassengers; + } + + public int getNumWheels() { + return numWheels; + } + + public void setNumWheels(int numWheels) { + this.numWheels = numWheels; + } + + public int getId() { + return id; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String toString() { + return "Make: "+make+", Model: "+model+", Number of passengers: "+numPassengers; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicleStatelessBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicleStatelessBean.java new file mode 100644 index 0000000..94d27db --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/RoadVehicleStatelessBean.java @@ -0,0 +1,50 @@ +package examples.entity.single_table; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.single_table.Coupe.BoringFactor; +import examples.entity.single_table.Roadster.CoolFactor; +import examples.entity.single_table.interfaces.RoadVehicleStateless; + +@Stateless +public class RoadVehicleStatelessBean implements RoadVehicleStateless { + @PersistenceContext(unitName="pu1") + EntityManager em; + + public void doSomeStuff() { + Coupe c = new Coupe(); + c.setMake("Bob"); + c.setModel("E400"); + c.setBoringFactor(BoringFactor.BORING); + em.persist(c); + + Roadster r = new Roadster(); + r.setMake("Mini"); + r.setModel("Cooper S"); + r.setCoolFactor(CoolFactor.COOLEST); + em.persist(r); + + Motorcycle m = new Motorcycle(); + em.persist(m); + } + + public List getAllRoadVehicles() { + Query q = em.createQuery("SELECT r FROM RoadVehicleSingle r"); + return q.getResultList(); + } + + public void deleteAll(String type) { + Query q = em.createQuery("DELETE FROM "+type); + q.executeUpdate(); + } + + public void updateAll(String type) { + Query q = em.createQuery("UPDATE "+type+" r SET r.numPassengers = 1"); + q.executeUpdate(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Roadster.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Roadster.java new file mode 100644 index 0000000..ae308ac --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/Roadster.java @@ -0,0 +1,31 @@ +package examples.entity.single_table; + +import java.io.Serializable; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity(name="RaodsterSingle") +@DiscriminatorValue("ROADSTER") +public class Roadster extends Car implements Serializable { + public enum CoolFactor {COOL,COOLER,COOLEST}; + + private CoolFactor coolFactor; + + public Roadster() { + super(); + numPassengers = 2; + } + + public CoolFactor getCoolFactor() { + return coolFactor; + } + + public void setCoolFactor(CoolFactor coolFactor) { + this.coolFactor = coolFactor; + } + + public String toString() { + return "Roadster: "+super.toString(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/client/RoadVehicleClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/client/RoadVehicleClient.java new file mode 100644 index 0000000..6ef6ea6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/client/RoadVehicleClient.java @@ -0,0 +1,54 @@ +package examples.entity.single_table.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.single_table.interfaces.RoadVehicleStateless; + + +public class RoadVehicleClient { + public static void main(String[] args) { + String action = "insert"; + String type = "RoadVehicleSingle"; + + if (args.length>0) { + if (args[0].startsWith("update")) { + action="update"; + } + else if (args[0].startsWith("delete")) { + action="delete"; + } + + if (args.length == 2) { + type = args[1]; + } + } + + InitialContext ic; + try { + ic = new InitialContext(); + RoadVehicleStateless rvs = (RoadVehicleStateless)ic.lookup(RoadVehicleStateless.class.getName()); + + if (action.equals("insert")) { + System.out.println("Inserting..."); + rvs.doSomeStuff(); + } + else if (action.equals("update")) { + System.out.println("Updating "+type+"..."); + rvs.updateAll(type); + } + else if (action.equals("delete")) { + System.out.println("Deleting "+type+"..."); + rvs.deleteAll(type); + } + + System.out.println("Here is the list of all RoadVehicles:\n"); + for (Object o : rvs.getAllRoadVehicles()) { + System.out.println("RoadVehicle: "+o); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/interfaces/RoadVehicleStateless.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/interfaces/RoadVehicleStateless.java new file mode 100644 index 0000000..c85da7e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/single_table/interfaces/RoadVehicleStateless.java @@ -0,0 +1,16 @@ +package examples.entity.single_table.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface RoadVehicleStateless { + public void doSomeStuff(); + + public List getAllRoadVehicles(); + + public void deleteAll(String type); + + public void updateAll(String type); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Course.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Course.java new file mode 100644 index 0000000..b922a7d --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Course.java @@ -0,0 +1,44 @@ +package examples.entity.uni.many_to_many; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity(name="CourseUni") +public class Course implements Serializable { + private int id; + private String courseName; + private Collection students = new ArrayList(); + + public Course() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + public Collection getStudents() { + return students; + } + + public void setStudents(Collection students) { + this.students = students; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Student.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Student.java new file mode 100644 index 0000000..a6568b1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/Student.java @@ -0,0 +1,50 @@ +package examples.entity.uni.many_to_many; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + +@Entity(name="StudentUni") +public class Student implements Serializable { + private int id; + private String name; + private Collection courses = new ArrayList(); + + public Student() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER) + @JoinTable(name="STUDENTUNI_COURSEUNI") + public Collection getCourses() { + return courses; + } + + public void setCourses(Collection courses) { + this.courses = courses; + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/StudentCourseUniBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/StudentCourseUniBean.java new file mode 100644 index 0000000..653133b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/StudentCourseUniBean.java @@ -0,0 +1,48 @@ +package examples.entity.uni.many_to_many; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.uni.many_to_many.interfaces.StudentCourse; + +@Stateless +public class StudentCourseUniBean implements StudentCourse { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Course c1 = new Course(); + c1.setCourseName("EJB 3.0 101"); + + Course c2 = new Course(); + c2.setCourseName("EJB 3.0 202"); + + Student s1 = new Student(); + s1.setName("Micah"); + + s1.getCourses().add(c1); + + c1.getStudents().add(s1); + + Student s2 = new Student(); + s2.setName("Tes"); + + s2.getCourses().add(c1); + s2.getCourses().add(c2); + + c1.getStudents().add(s2); + c2.getStudents().add(s2); + + em.persist(s1); + em.persist(s2); + } + + public List getAllStudents() { + Query q = em.createQuery("SELECT s FROM StudentUni s"); + return q.getResultList(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/client/StudentCourseClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/client/StudentCourseClient.java new file mode 100644 index 0000000..d18aee2 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/client/StudentCourseClient.java @@ -0,0 +1,30 @@ +package examples.entity.uni.many_to_many.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.uni.many_to_many.Course; +import examples.entity.uni.many_to_many.Student; +import examples.entity.uni.many_to_many.interfaces.StudentCourse; + + +public class StudentCourseClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + StudentCourse sc = (StudentCourse)ic.lookup(StudentCourse.class.getName()); + + sc.doSomeStuff(); + + for (Student s : sc.getAllStudents()) { + System.out.println("Student: "+s.getName()); + for (Course c : s.getCourses()) { + System.out.println("\tCourse: "+c.getCourseName()); + } + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/interfaces/StudentCourse.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/interfaces/StudentCourse.java new file mode 100644 index 0000000..65e9cef --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many/interfaces/StudentCourse.java @@ -0,0 +1,14 @@ +package examples.entity.uni.many_to_many.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +import examples.entity.uni.many_to_many.Student; + +@Remote +public interface StudentCourse { + public void doSomeStuff(); + + public List getAllStudents(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/BusinessAddress.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/BusinessAddress.java new file mode 100644 index 0000000..755cd6c --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/BusinessAddress.java @@ -0,0 +1,42 @@ +package examples.entity.uni.many_to_one; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class BusinessAddress implements Serializable { + private int id; + private String city; + private String zipcode; + + public BusinessAddress() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/Employee.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/Employee.java new file mode 100644 index 0000000..f14104f --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/Employee.java @@ -0,0 +1,45 @@ +package examples.entity.uni.many_to_one; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Employee implements Serializable { + private int id; + private String name; + private BusinessAddress address; + + public Employee() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @ManyToOne(cascade={CascadeType.ALL}) + public BusinessAddress getAddress() { + return address; + } + + public void setAddress(BusinessAddress address) { + this.address = address; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/EmployeeAddressMOUniBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/EmployeeAddressMOUniBean.java new file mode 100644 index 0000000..be74d18 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/EmployeeAddressMOUniBean.java @@ -0,0 +1,42 @@ +package examples.entity.uni.many_to_one; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.uni.many_to_one.interfaces.EmployeeAddressMOUni; + +@Stateless +public class EmployeeAddressMOUniBean implements EmployeeAddressMOUni { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + BusinessAddress a = new BusinessAddress(); + a.setCity("Huntington Station"); + a.setZipcode("11746"); + + Employee e = new Employee(); + e.setName("Micah Silverman"); + e.setAddress(a); + em.persist(e); + + e = new Employee(); + e.setName("Tes Silverman"); + e.setAddress(a); + em.persist(e); + + e = new Employee(); + e.setName("Shaina Silverman"); + e.setAddress(a); + em.persist(e); + } + + public List getEmployees() { + Query q = em.createQuery("SELECT e FROM Employee e"); + return q.getResultList(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/client/EmployeeAddressClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/client/EmployeeAddressClient.java new file mode 100644 index 0000000..efabba5 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/client/EmployeeAddressClient.java @@ -0,0 +1,28 @@ +package examples.entity.uni.many_to_one.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.uni.many_to_one.Employee; +import examples.entity.uni.many_to_one.interfaces.EmployeeAddressMOUni; + + +public class EmployeeAddressClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + EmployeeAddressMOUni ea = (EmployeeAddressMOUni)ic.lookup(EmployeeAddressMOUni.class.getName()); + + ea.doSomeStuff(); + + for (Object o : ea.getEmployees()) { + Employee e = (Employee)o; + System.out.println("Name: "+e.getName()+", Business Address: "+ + e.getAddress().getCity()+", "+e.getAddress().getZipcode()); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/interfaces/EmployeeAddressMOUni.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/interfaces/EmployeeAddressMOUni.java new file mode 100644 index 0000000..904a3be --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_one/interfaces/EmployeeAddressMOUni.java @@ -0,0 +1,12 @@ +package examples.entity.uni.many_to_one.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface EmployeeAddressMOUni { + public void doSomeStuff(); + + public List getEmployees(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Company.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Company.java new file mode 100644 index 0000000..ad5554b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Company.java @@ -0,0 +1,47 @@ +package examples.entity.uni.one_to_many; + +import java.io.Serializable; +import java.util.Collection; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity(name="CompanyOMUni") +public class Company implements Serializable { + private int id; + private String name; + private Collection employees; + + public Company() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER) + public Collection getEmployees() { + return employees; + } + + public void setEmployees(Collection employees) { + this.employees = employees; + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/CompanyEmployeeOMUniBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/CompanyEmployeeOMUniBean.java new file mode 100644 index 0000000..a7e5ffa --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/CompanyEmployeeOMUniBean.java @@ -0,0 +1,73 @@ +package examples.entity.uni.one_to_many; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.uni.one_to_many.interfaces.CompanyEmployeeOM; + +@Stateless +public class CompanyEmployeeOMUniBean implements CompanyEmployeeOM { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Company c = new Company(); + c.setName("M*Power Internet Services, Inc."); + + Collection employees = new ArrayList(); + Employee e = new Employee(); + e.setName("Micah Silverman"); + e.setSex('M'); + employees.add(e); + + e = new Employee(); + e.setName("Tes Silverman"); + e.setSex('F'); + employees.add(e); + + c.setEmployees(employees); + em.persist(c); + + c = new Company(); + c.setName("Sun Microsystems"); + + employees = new ArrayList(); + e = new Employee(); + e.setName("Rima Patel"); + e.setSex('F'); + employees.add(e); + + e = new Employee(); + e.setName("James Gosling"); + e.setSex('M'); + employees.add(e); + + c.setEmployees(employees); + em.persist(c); + + c = new Company(); + c.setName("Bob's Bait & Tackle"); + em.persist(c); + } + + public List getCompanies() { + Query q = em.createQuery("SELECT c FROM CompanyOMUni c"); + return q.getResultList(); + } + + public List getCompanies2(String query) { + Query q = em.createQuery(query); + return q.getResultList(); + } + + public void deleteCompanies() { + Query q = em.createQuery("DELETE FROM CompanyOMUni"); + q.executeUpdate(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Employee.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Employee.java new file mode 100644 index 0000000..c849223 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/Employee.java @@ -0,0 +1,42 @@ +package examples.entity.uni.one_to_many; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity(name="EmployeeOMUni") +public class Employee implements Serializable { + private int id; + private String name; + private char sex; + + public Employee() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public char getSex() { + return sex; + } + + public void setSex(char sex) { + this.sex = sex; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/client/CompanyEmployeeClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/client/CompanyEmployeeClient.java new file mode 100644 index 0000000..6077c1a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/client/CompanyEmployeeClient.java @@ -0,0 +1,35 @@ +package examples.entity.uni.one_to_many.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.uni.one_to_many.Company; +import examples.entity.uni.one_to_many.Employee; +import examples.entity.uni.one_to_many.interfaces.CompanyEmployeeOM; + + +public class CompanyEmployeeClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + CompanyEmployeeOM ceom = (CompanyEmployeeOM)ic.lookup(CompanyEmployeeOM.class.getName()); + + ceom.deleteCompanies(); + + ceom.doSomeStuff(); + + System.out.println("All Companies:"); + for (Object o : ceom.getCompanies()) { + Company c = (Company)o; + System.out.println("Here are the employees for company: "+c.getName()); + for (Employee e : c.getEmployees()) { + System.out.println("\tName: "+e.getName()+", Sex: "+e.getSex()); + } + System.out.println(); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/interfaces/CompanyEmployeeOM.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/interfaces/CompanyEmployeeOM.java new file mode 100644 index 0000000..7da70f3 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many/interfaces/CompanyEmployeeOM.java @@ -0,0 +1,16 @@ +package examples.entity.uni.one_to_many.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface CompanyEmployeeOM { + public void doSomeStuff(); + + public List getCompanies(); + + public List getCompanies2(String query); + + public void deleteCompanies(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Order.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Order.java new file mode 100644 index 0000000..71adb1e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Order.java @@ -0,0 +1,45 @@ +package examples.entity.uni.one_to_one; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity(name="OrderUni") +public class Order implements Serializable { + private int id; + private String orderName; + private Shipment shipment; + + public Order() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getOrderName() { + return orderName; + } + + public void setOrderName(String orderName) { + this.orderName = orderName; + } + + @OneToOne(cascade={CascadeType.PERSIST}) + public Shipment getShipment() { + return shipment; + } + + public void setShipment(Shipment shipment) { + this.shipment = shipment; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/OrderShipmentUniBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/OrderShipmentUniBean.java new file mode 100644 index 0000000..9399996 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/OrderShipmentUniBean.java @@ -0,0 +1,33 @@ +package examples.entity.uni.one_to_one; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import examples.entity.uni.one_to_one.interfaces.OrderShipment; + +@Stateless +public class OrderShipmentUniBean implements OrderShipment { + @PersistenceContext + EntityManager em; + + public void doSomeStuff() { + Shipment s = new Shipment(); + s.setCity("Austin"); + s.setZipcode("78727"); + + Order o = new Order(); + o.setOrderName("Software Order"); + o.setShipment(s); + + em.persist(o); + } + + public List getOrders() { + Query q = em.createQuery("SELECT o FROM OrderUni o"); + return q.getResultList(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Shipment.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Shipment.java new file mode 100644 index 0000000..4f16169 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/Shipment.java @@ -0,0 +1,42 @@ +package examples.entity.uni.one_to_one; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity(name="ShipmentUni") +public class Shipment implements Serializable { + private int id; + private String city; + private String zipcode; + + public Shipment() { + id = (int)System.nanoTime(); + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/client/OrderShipmentClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/client/OrderShipmentClient.java new file mode 100644 index 0000000..666fc5d --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/client/OrderShipmentClient.java @@ -0,0 +1,30 @@ +package examples.entity.uni.one_to_one.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.entity.uni.one_to_one.Order; +import examples.entity.uni.one_to_one.interfaces.OrderShipment; + + +public class OrderShipmentClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + OrderShipment os = (OrderShipment)ic.lookup(OrderShipment.class.getName()); + + os.doSomeStuff(); + + System.out.println("Unidirectional One-To-One client\n"); + + for (Object o : os.getOrders()) { + Order order = (Order)o; + System.out.println("Order "+order.getId()+": "+order.getOrderName()); + System.out.println("\tShipment details: "+order.getShipment().getCity()+" "+order.getShipment().getZipcode()); + } + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/interfaces/OrderShipment.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/interfaces/OrderShipment.java new file mode 100644 index 0000000..c697a10 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_one/interfaces/OrderShipment.java @@ -0,0 +1,12 @@ +package examples.entity.uni.one_to_one.interfaces; + +import java.util.List; + +import javax.ejb.Remote; + +@Remote +public interface OrderShipment { + public void doSomeStuff(); + + public List getOrders(); +} -- cgit v1.2.3