1 package org.hibernate.test.annotations.query; 3 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import java.util.GregorianCalendar ; 7 import java.util.List ; 8 9 import org.hibernate.Query; 10 import org.hibernate.Session; 11 import org.hibernate.Transaction; 12 import org.hibernate.test.annotations.A320; 13 import org.hibernate.test.annotations.A320b; 14 import org.hibernate.test.annotations.Plane; 15 import org.hibernate.test.annotations.TestCase; 16 17 22 public class QueryTest extends TestCase { 23 public QueryTest(String x) { 24 super(x); 25 } 26 27 public void testPackageQueries() throws Exception { 28 Session s = openSession(); 29 Transaction tx = s.beginTransaction(); 30 Plane p = new Plane(); 31 s.persist(p); 32 Query q = s.getNamedQuery("plane.getAll"); 33 assertEquals( 1, q.list().size() ); 34 tx.commit(); 35 s.close(); 36 } 37 38 public void testClassQueries() throws Exception { 39 Session s = openSession(); 40 Transaction tx = s.beginTransaction(); 41 Night n = new Night(); 42 Calendar c = new GregorianCalendar (); 43 c.set(2000, 2, 2); 44 Date now = c.getTime(); 45 c.add(Calendar.MONTH, -1); 46 Date aMonthAgo = c.getTime(); 47 c.add(Calendar.MONTH, 2); 48 Date inAMonth = c.getTime(); 49 n.setDate( now ); 50 n.setDuration(14); 51 s.persist(n); 52 Query q = s.getNamedQuery("night.moreRecentThan"); 53 q.setDate( "date", aMonthAgo ); 54 assertEquals( 1, q.list().size() ); 55 q = s.getNamedQuery("night.moreRecentThan"); 56 q.setDate( "date", inAMonth ); 57 assertEquals( 0, q.list().size() ); 58 tx.commit(); 59 s.close(); 60 } 61 62 public void testSQLQuery() { 63 Night n = new Night(); 64 Calendar c = new GregorianCalendar (); 65 c.set(2000, 2, 2); 66 Date now = c.getTime(); 67 c.add(Calendar.MONTH, -1); 68 Date aMonthAgo = c.getTime(); 69 c.add(Calendar.MONTH, 2); 70 Date inAMonth = c.getTime(); 71 n.setDate( now ); 72 n.setDuration(9999); 73 Area area = new Area(); 74 area.setName("Monceau"); 75 76 Session s = openSession(); 77 Transaction tx = s.beginTransaction(); 78 s.persist(n); 79 s.persist(area); 80 tx.commit(); 81 s.clear(); 82 tx = s.beginTransaction(); 83 Query q = s.getNamedQuery("night.getAll.bySQL"); 84 q.setParameter(0, 9990); 85 List result = q.list(); 86 assertEquals( 1, result.size() ); 87 Night n2 = (Night) result.get(0); 88 assertEquals( n2.getDuration(), n.getDuration() ); 89 List areas = s.getNamedQuery("getAreaByNative").list(); 90 assertTrue( 1 == areas.size() ); 91 assertEquals( area.getName(), ( (Area) areas.get(0) ).getName() ); 92 tx.commit(); 93 s.close(); 94 } 95 96 public void testSQLQueryWithManyToOne() { 97 Night n = new Night(); 98 Calendar c = new GregorianCalendar (); 99 c.set(2000, 2, 2); 100 Date now = c.getTime(); 101 c.add(Calendar.MONTH, -1); 102 Date aMonthAgo = c.getTime(); 103 c.add(Calendar.MONTH, 2); 104 Date inAMonth = c.getTime(); 105 n.setDate( now ); 106 n.setDuration(9999); 107 Area a = new Area(); 108 a.setName("Paris"); 109 n.setArea(a); 110 Session s = openSession(); 111 Transaction tx = s.beginTransaction(); 112 s.persist(a); 113 s.persist(n); 114 tx.commit(); 115 s.clear(); 116 tx = s.beginTransaction(); 117 Query q = s.getNamedQuery("night&area"); 118 List result = q.list(); 119 assertEquals( 1, result.size() ); 120 Night n2 = (Night) ( (Object []) result.get(0) )[0]; 121 assertEquals( n2.getDuration(), n.getDuration() ); 122 tx.commit(); 123 s.close(); 124 } 125 126 public void testImplicitNativeQuery() throws Exception { 127 Session s; 128 Transaction tx; 129 s = openSession(); 130 tx = s.beginTransaction(); 131 SpaceShip ship = new SpaceShip(); 132 ship.setModel("X-Wing"); 133 ship.setName("YuBlue"); 134 ship.setSpeed(2000); 135 s.persist(ship); 136 tx.commit(); 137 s.clear(); 138 tx = s.beginTransaction(); 139 Query q = s.getNamedQuery("implicitSample"); 140 List result = q.list(); 141 assertEquals( 1, result.size() ); 142 assertEquals( ship.getModel(), ( (SpaceShip) result.get(0) ).getModel() ); 143 tx.commit(); 144 s.close(); 145 } 146 147 public void testDiscriminator() throws Exception { 148 Session s; 149 Transaction tx; 150 s = openSession(); 151 tx = s.beginTransaction(); 152 Dictionary dic = new Dictionary(); 153 dic.setName("Anglais-Francais"); 154 dic.setEditor("Harrap's"); 155 SynonymousDictionary syn = new SynonymousDictionary(); 156 syn.setName("Synonymes de tous les temps"); 157 syn.setEditor("Imagination edition"); 158 s.persist(dic); 159 s.persist(syn); 160 tx.commit(); 161 s.clear(); 162 tx = s.beginTransaction(); 163 List results = s.getNamedQuery("all.dictionaries").list(); 164 assertEquals( 2, results.size() ); 165 assertTrue( results.get(0) instanceof SynonymousDictionary 166 || results.get(1) instanceof SynonymousDictionary); 167 tx.commit(); 168 s.close(); 169 } 170 171 191 protected Class [] getMappings() { 192 return new Class [] { 193 Plane.class, 194 A320.class, 195 A320b.class, 196 Night.class, 197 Area.class, 198 SpaceShip.class, 199 Dictionary.class, 200 SynonymousDictionary.class 201 }; 202 } 203 204 protected String [] getAnnotatedPackages() { 205 return new String [] { 206 "org.hibernate.test.annotations.query" 207 }; 208 } 209 } 210 | Popular Tags |