diff options
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/entity/uni/many_to_many')
5 files changed, 186 insertions, 0 deletions
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<Student> students = new ArrayList<Student>();
+
+ 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<Student> getStudents() {
+ return students;
+ }
+
+ public void setStudents(Collection<Student> 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<Course> courses = new ArrayList<Course>();
+
+ 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<Course> getCourses() {
+ return courses;
+ }
+
+ public void setCourses(Collection<Course> 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<Student> 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<Student> getAllStudents();
+}
|
