Author: max.andersen(a)jboss.com
Date: 2006-09-21 17:10:01 -0400 (Thu, 21 Sep 2006)
New Revision: 10509
Added:
trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/CascadeTest.java
trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Student.java
trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Teacher.java
Log:
test showing massive cascading
Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/CascadeTest.java
===================================================================
---
trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/CascadeTest.java 2006-09-21
14:41:33 UTC (rev 10508)
+++
trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/CascadeTest.java 2006-09-21
21:10:01 UTC (rev 10509)
@@ -0,0 +1,66 @@
+//$Id: FetchTest.java 9796 2006-04-26 06:46:52Z epbernard $
+package org.hibernate.ejb.test.cascade;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.hibernate.ejb.test.TestCase;
+
+
+/**
+ * @author Max Rydahl Andersen
+ */
+public class CascadeTest extends TestCase {
+
+ public void testCascade() throws Exception {
+
+ EntityManager em = factory.createEntityManager();
+ em.getTransaction().begin();
+
+ Teacher teacher = null;
+
+ teacher = new Teacher();
+
+ Student student = new Student();
+
+ teacher.setFavoriteStudent(student);
+ student.setFavoriteTeacher(teacher);
+
+ teacher.getStudents().add(student);
+ student.setPrimaryTeacher(teacher);
+
+ em.persist( teacher );
+ em.getTransaction().commit();
+
+ System.out.println("***************************");
+ em = factory.createEntityManager();
+ em.getTransaction().begin();
+
+ Teacher foundTeacher = (Teacher) em.createQuery( "select t from Teacher as t"
).getSingleResult();
+
+ System.out.println(foundTeacher);
+ System.out.println(foundTeacher.getFavoriteStudent());
+
+ for (Student fstudent : foundTeacher.getStudents()) {
+ System.out.println(fstudent);
+ System.out.println(fstudent.getFavoriteTeacher());
+ System.out.println(fstudent.getPrimaryTeacher());
+ }
+
+ em.getTransaction().commit(); // here *alot* of flushes occur on an object graph that
has *Zero* changes.
+ em.close();
+
+
+ }
+
+
+ public Class[] getAnnotatedClasses() {
+ return new Class[]{
+ Teacher.class,
+ Student.class
+ };
+ }
+
+
+}
Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Student.java
===================================================================
--- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Student.java 2006-09-21
14:41:33 UTC (rev 10508)
+++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Student.java 2006-09-21
21:10:01 UTC (rev 10509)
@@ -0,0 +1,57 @@
+package org.hibernate.ejb.test.cascade;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.OneToOne;
+
+import org.hibernate.annotations.AccessType;
+
+@Entity
+@AccessType("field")
+public class Student {
+
+ @Id @GeneratedValue
+ Long id;
+
+ String name;
+
+ @ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST})
+ private Teacher primaryTeacher;
+
+ @OneToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST})
+ private Teacher favoriteTeacher;
+
+ public Student() {
+ }
+
+ public Teacher getFavoriteTeacher() {
+ return favoriteTeacher;
+ }
+
+ public void setFavoriteTeacher(Teacher lifeCover) {
+ this.favoriteTeacher = lifeCover;
+ }
+
+ public Teacher getPrimaryTeacher() {
+ return primaryTeacher;
+ }
+
+ public void setPrimaryTeacher(Teacher relativeTo) {
+ this.primaryTeacher = relativeTo;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Teacher.java
===================================================================
--- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Teacher.java 2006-09-21
14:41:33 UTC (rev 10508)
+++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/cascade/Teacher.java 2006-09-21
21:10:01 UTC (rev 10509)
@@ -0,0 +1,62 @@
+package org.hibernate.ejb.test.cascade;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+
+import org.hibernate.annotations.AccessType;
+
+@Entity
+@AccessType("field")
+public class Teacher {
+
+ @Id @GeneratedValue
+ Long id;
+
+ String name;
+
+ @OneToMany(mappedBy="primaryTeacher", cascade={CascadeType.MERGE,
CascadeType.PERSIST})
+ private Set<Student> students = new HashSet<Student>();
+
+ @OneToOne(mappedBy="favoriteTeacher", cascade={CascadeType.MERGE,
CascadeType.PERSIST})
+ private Student favoriteStudent;
+
+ public Teacher() {
+ }
+
+ public Student getFavoriteStudent() {
+ return favoriteStudent;
+ }
+
+ public void setFavoriteStudent(
+ Student contributionOrBenefitParameters) {
+ this.favoriteStudent = contributionOrBenefitParameters;
+ }
+
+ public Set<Student> getStudents() {
+ return students;
+ }
+
+ public void setStudents(
+ Set<Student> todoCollection) {
+ this.students = todoCollection;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Show replies by date