1 package org.hibernate.test.annotations.cascade; 3 4 import org.hibernate.Session; 5 import org.hibernate.Transaction; 6 import org.hibernate.test.annotations.TestCase; 7 8 import java.util.ArrayList ; 9 10 14 public class CascadeTest extends TestCase { 16 public void testPersist() { 17 Session s; 18 Transaction tx; 19 s = openSession(); 20 Tooth tooth = new Tooth(); 21 Tooth leftTooth = new Tooth(); 22 tooth.leftNeighbour = leftTooth; 23 s.persist(tooth); 24 tx = s.beginTransaction(); 25 tx.commit(); 26 s.close(); 27 28 s = openSession(); 29 tx = s.beginTransaction(); 30 leftTooth = (Tooth) s.get(Tooth.class, leftTooth.id); 31 assertNotNull(leftTooth); 32 tx.commit(); 33 s.close(); 34 } 35 36 public void testMerge() { 37 Session s; 38 Transaction tx; 39 s = openSession(); 40 Tooth tooth = new Tooth(); 41 Tooth rightTooth = new Tooth(); 42 tooth.type = "canine"; 43 tooth.rightNeighbour = rightTooth; 44 rightTooth.type = "incisive"; 45 s.persist(rightTooth); 46 s.persist(tooth); 47 tx = s.beginTransaction(); 48 tx.commit(); 49 s.close(); 50 51 s = openSession(); 52 tx = s.beginTransaction(); 53 tooth = (Tooth) s.get(Tooth.class, tooth.id); 54 assertEquals( "incisive", tooth.rightNeighbour.type ); 55 tx.commit(); 56 s.close(); 57 58 s = openSession(); 59 tx = s.beginTransaction(); 60 tooth.rightNeighbour.type = "premolars"; 61 s.merge(tooth); 62 tx.commit(); 63 s.close(); 64 65 s = openSession(); 66 tx = s.beginTransaction(); 67 rightTooth = (Tooth) s.get(Tooth.class, rightTooth.id); 68 assertEquals( "premolars", rightTooth.type ); 69 tx.commit(); 70 s.close(); 71 } 72 73 public void testRemove() { 74 Session s; 75 Transaction tx; 76 s = openSession(); 77 tx = s.beginTransaction(); 78 Tooth tooth = new Tooth(); 79 Mouth mouth = new Mouth(); 80 s.persist(mouth); 81 s.persist(tooth); 82 tooth.mouth = mouth; 83 mouth.teeth = new ArrayList <Tooth>(); 84 mouth.teeth.add(tooth); 85 tx.commit(); 86 s.close(); 87 88 s = openSession(); 89 tx = s.beginTransaction(); 90 tooth = (Tooth) s.get(Tooth.class, tooth.id); 91 assertNotNull(tooth); 92 s.delete( tooth.mouth ); 93 tx.commit(); 94 s.close(); 95 96 s = openSession(); 97 tx = s.beginTransaction(); 98 tooth = (Tooth) s.get(Tooth.class, tooth.id); 99 assertNull(tooth); 100 tx.commit(); 101 s.close(); 102 } 103 104 public CascadeTest(String x) { 105 super(x); 106 } 107 108 protected Class [] getMappings() { 109 return new Class [] { 110 Mouth.class, 111 Tooth.class 112 }; 113 } 114 } 115 | Popular Tags |