1 package org.hibernate.test.annotations.join; 3 4 import java.util.Date ; 5 6 import org.hibernate.Criteria; 7 import org.hibernate.HibernateException; 8 import org.hibernate.Query; 9 import org.hibernate.Session; 10 import org.hibernate.Transaction; 11 import org.hibernate.criterion.Expression; 12 import org.hibernate.test.annotations.TestCase; 13 14 17 public class JoinTest extends TestCase { 18 19 public JoinTest(String x) { 20 super(x); 21 } 22 23 public void testDefaultValue() throws Exception { 24 Session s = openSession(); 25 Transaction tx = s.beginTransaction(); 26 Life life = new Life(); 27 life.duration = 15; 28 life.fullDescription = "Long long description"; 29 s.persist(life); 30 tx.commit(); 31 s.close(); 32 33 s = openSession(); 34 tx = s.beginTransaction(); 35 Query q = s.createQuery( "from " + Life.class.getName() ); 36 life = (Life) q.uniqueResult(); 37 assertEquals("Long long description", life.fullDescription); 38 tx.commit(); 39 s.close(); 40 } 41 42 public void testCompositePK() throws Exception { 43 Session s = openSession(); 44 Transaction tx = s.beginTransaction(); 45 Dog dog = new Dog(); 46 DogPk id = new DogPk(); 47 id.name = "Thalie"; 48 id.ownerName = "Martine"; 49 dog.id = id; 50 dog.weight = 30; 51 dog.thoroughbredName = "Colley"; 52 s.persist(dog); 53 tx.commit(); 54 s.close(); 55 56 s = openSession(); 57 tx = s.beginTransaction(); 58 Query q = s.createQuery( "from Dog" ); 59 dog = (Dog) q.uniqueResult(); 60 assertEquals("Colley", dog.thoroughbredName); 61 tx.commit(); 62 s.close(); 63 } 64 65 public void testExplicitValue() throws Exception { 66 Session s = openSession(); 67 Transaction tx = s.beginTransaction(); 68 Death death = new Death(); 69 death.date = new Date (); 70 death.howDoesItHappen = "Well, haven't seen it"; 71 s.persist(death); 72 tx.commit(); 73 s.close(); 74 75 s = openSession(); 76 tx = s.beginTransaction(); 77 Query q = s.createQuery( "from " + Death.class.getName() ); 78 death = (Death) q.uniqueResult(); 79 assertEquals("Well, haven't seen it", death.howDoesItHappen); 80 s.delete(death); 81 tx.commit(); 82 s.close(); 83 } 84 85 public void testManyToOne() throws Exception { 86 Session s = openSession(); 87 Transaction tx = s.beginTransaction(); 88 Life life = new Life(); 89 Cat cat = new Cat(); 90 cat.setName("kitty"); 91 life.duration = 15; 92 life.fullDescription = "Long long description"; 93 life.owner = cat; 94 s.persist(life); 95 tx.commit(); 96 s.close(); 97 98 s = openSession(); 99 tx = s.beginTransaction(); 100 Criteria crit = s.createCriteria(Life.class); 101 crit.createCriteria("owner").add(Expression.eq("name", "kitty") ); 102 life = (Life) crit.uniqueResult(); 103 assertEquals("Long long description", life.fullDescription); 104 s.delete(life.owner); 105 s.delete(life); 106 tx.commit(); 107 s.close(); 108 } 109 110 public void testUniqueConstaintOnSecondaryTable() throws Exception { 111 Cat cat = new Cat(); 112 cat.setStoryPart2("My long story"); 113 Cat cat2 = new Cat(); 114 cat2.setStoryPart2("My long story"); 115 Session s = openSession(); 116 Transaction tx = s.beginTransaction(); 117 try { 118 s.persist(cat); 119 s.persist(cat2); 120 tx.commit(); 121 fail("unique constraints violation on secondary table"); 122 } 123 catch (HibernateException e) { 124 } finally { 126 if (tx != null) tx.rollback(); 127 s.close(); 128 } 129 } 130 131 134 protected Class [] getMappings() { 135 return new Class [] { 136 Life.class, 137 Death.class, 138 Cat.class, 139 Dog.class 140 }; 141 } 142 143 } 144 | Popular Tags |