diff options
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/one_to_many')
5 files changed, 213 insertions, 0 deletions
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<Employee> 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<Employee> getEmployees() {
+ return employees;
+ }
+
+ public void setEmployees(Collection<Employee> 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<Employee> employees = new ArrayList<Employee>();
+ 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<Employee>();
+ 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();
+}
|
