KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > hql > AdHocOnTest


1 // $Id: AdHocOnTest.java,v 1.3 2005/06/15 23:16:17 oneovthafew Exp $
2
package org.hibernate.test.hql;
3
4 import org.hibernate.test.TestCase;
5 import org.hibernate.Session;
6 import org.hibernate.Transaction;
7 import org.hibernate.HibernateException;
8
9 import java.util.List JavaDoc;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13
14 /**
15  * Implementation of AdHocOnTest.
16  *
17  * @author Steve Ebersole
18  */

19 public class AdHocOnTest extends TestCase {
20
21     public AdHocOnTest(String JavaDoc name) {
22         super( name );
23     }
24
25     protected String JavaDoc[] getMappings() {
26         return new String JavaDoc[] { "hql/Animal.hbm.xml" };
27     }
28
29     public static Test suite() {
30         return new TestSuite( AdHocOnTest.class );
31     }
32
33     public void testAdHocOnFailsWithFetch() {
34         TestData data = new TestData();
35         data.prepare();
36
37         Session s = openSession();
38         Transaction txn = s.beginTransaction();
39
40         try {
41             s.createQuery( "from Animal a inner join fetch a.offspring as o with o.bodyWeight = :someLimit" )
42                     .setDouble( "someLimit", 1 )
43                     .list();
44             fail( "ad-hoc on clause allowed with fetched association" );
45         }
46         catch ( HibernateException e ) {
47             System.out.println( "TEST (OK) : " + e.getMessage() );
48             // the expected response...
49
}
50
51         txn.commit();
52         s.close();
53
54         data.cleanup();
55     }
56
57     public void testAdHocOn() {
58         TestData data = new TestData();
59         data.prepare();
60
61         Session s = openSession();
62         Transaction txn = s.beginTransaction();
63
64         List JavaDoc list = s.createQuery( "from Animal a inner join a.offspring as o with o.bodyWeight < :someLimit" )
65                 .setDouble( "someLimit", 1 )
66                 .list();
67         assertTrue( "ad-hoc on did not take effect", list.isEmpty() );
68
69         list = s.createQuery( "from Animal a inner join a.mother as m with m.bodyWeight < :someLimit" )
70                 .setDouble( "someLimit", 1 )
71                 .list();
72         assertTrue( "ad-hoc on did not take effect", list.isEmpty() );
73
74         txn.commit();
75         s.close();
76
77         data.cleanup();
78     }
79
80     private class TestData {
81         public void prepare() {
82             Session session = openSession();
83             Transaction txn = session.beginTransaction();
84
85             Animal mother = new Animal();
86             mother.setBodyWeight( 10 );
87             mother.setDescription( "mother" );
88
89             Animal father = new Animal();
90             father.setBodyWeight( 15 );
91             father.setDescription( "father" );
92
93             Animal child1 = new Animal();
94             child1.setBodyWeight( 5 );
95             child1.setDescription( "child1" );
96
97             Animal child2 = new Animal();
98             child2.setBodyWeight( 6 );
99             child2.setDescription( "child2" );
100
101             child1.setMother( mother );
102             child1.setFather( father );
103             mother.addOffspring( child1 );
104             father.addOffspring( child1 );
105
106             child2.setMother( mother );
107             child2.setFather( father );
108             mother.addOffspring( child2 );
109             father.addOffspring( child2 );
110
111             session.save( mother );
112             session.save( father );
113             session.save( child1 );
114             session.save( child2 );
115
116             txn.commit();
117             session.close();
118         }
119
120         public void cleanup() {
121             Session session = openSession();
122             Transaction txn = session.beginTransaction();
123             session.createQuery( "delete Animal where mother is not null" ).executeUpdate();
124             session.createQuery( "delete Animal" ).executeUpdate();
125             txn.commit();
126             session.close();
127         }
128     }
129 }
130
Popular Tags