Source code: org/hibernate/test/hql/WithClauseTest.java
1 // $Id: WithClauseTest.java 9335 2006-02-24 22:10:26Z steveebersole $
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 import org.hibernate.QueryException;
9 import org.hibernate.cfg.Configuration;
10 import org.hibernate.cfg.Environment;
11
12 import java.util.List;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18
19 /**
20 * Implementation of WithClauseTest.
21 *
22 * @author Steve Ebersole
23 */
24 public class WithClauseTest extends TestCase {
25
26 public WithClauseTest(String name) {
27 super( name );
28 }
29
30 protected String[] getMappings() {
31 return new String[] { "hql/Animal.hbm.xml" };
32 }
33
34 public static Test suite() {
35 return new TestSuite( WithClauseTest.class );
36 }
37
38 protected void configure(Configuration cfg) {
39 super.configure( cfg );
40 }
41
42 public void testWithClauseFailsWithFetch() {
43 TestData data = new TestData();
44 data.prepare();
45
46 Session s = openSession();
47 Transaction txn = s.beginTransaction();
48
49 try {
50 s.createQuery( "from Animal a inner join fetch a.offspring as o with o.bodyWeight = :someLimit" )
51 .setDouble( "someLimit", 1 )
52 .list();
53 fail( "ad-hoc on clause allowed with fetched association" );
54 }
55 catch ( HibernateException e ) {
56 // the expected response...
57 }
58
59 txn.commit();
60 s.close();
61
62 data.cleanup();
63 }
64
65 public void testInvalidWithSemantics() {
66 Session s = openSession();
67 Transaction txn = s.beginTransaction();
68
69 try {
70 // PROBLEM : f.bodyWeight is a reference to a column on the Animal table; however, the 'f'
71 // alias relates to the Human.friends collection which the aonther Human entity. The issue
72 // here is the way JoinSequence and Joinable (the persister) interact to generate the
73 // joins relating to the sublcass/superclass tables
74 s.createQuery( "from Human h inner join h.friends as f with f.bodyWeight < :someLimit" )
75 .setDouble( "someLimit", 1 )
76 .list();
77 fail( "failure expected" );
78 }
79 catch( QueryException qe ) {
80 if ( qe.getMessage().indexOf( "can only reference columns in the driving table" ) < 0 ) {
81 fail( "unexpected failure type [" + qe.getMessage() + "]" );
82 }
83 }
84
85 try {
86 s.createQuery( "from Animal a inner join a.offspring o inner join o.mother as m inner join m.father as f with o.bodyWeight > 1" )
87 .list();
88 fail( "failure expected" );
89 }
90 catch( QueryException qe ) {
91 if ( qe.getMessage().indexOf( "with-clause expressions did not reference from-clause element to which the with-clause was associated" ) < 0 ) {
92 fail( "unexpected failure type [" + qe.getMessage() + "]" );
93 }
94 }
95
96 try {
97 s.createQuery( "from Human h inner join h.offspring o with o.mother.father = :cousin" )
98 .setEntity( "cousin", s.load( Human.class, new Long(123) ) )
99 .list();
100 fail( "failure expected" );
101 }
102 catch( QueryException qe ) {
103 if ( qe.getMessage().indexOf( "with-clause expressions did not reference from-clause element to which the with-clause was associated" ) < 0 ) {
104 fail( "unexpected failure type [" + qe.getMessage() + "]" );
105 }
106 }
107
108 txn.commit();
109 s.close();
110 }
111
112 public void testWithClause() {
113 TestData data = new TestData();
114 data.prepare();
115
116 Session s = openSession();
117 Transaction txn = s.beginTransaction();
118
119 // one-to-many
120 List list = s.createQuery( "from Human h inner join h.offspring as o with o.bodyWeight < :someLimit" )
121 .setDouble( "someLimit", 1 )
122 .list();
123 assertTrue( "ad-hoc on did not take effect", list.isEmpty() );
124
125 // many-to-one
126 list = s.createQuery( "from Animal a inner join a.mother as m with m.bodyWeight < :someLimit" )
127 .setDouble( "someLimit", 1 )
128 .list();
129 assertTrue( "ad-hoc on did not take effect", list.isEmpty() );
130
131 // many-to-many
132 list = s.createQuery( "from Human h inner join h.friends as f with f.nickName like 'bubba'" )
133 .list();
134 assertTrue( "ad-hoc on did not take effect", list.isEmpty() );
135
136 txn.commit();
137 s.close();
138
139 data.cleanup();
140 }
141
142 private class TestData {
143 public void prepare() {
144 Session session = openSession();
145 Transaction txn = session.beginTransaction();
146
147 Human mother = new Human();
148 mother.setBodyWeight( 10 );
149 mother.setDescription( "mother" );
150
151 Human father = new Human();
152 father.setBodyWeight( 15 );
153 father.setDescription( "father" );
154
155 Human child1 = new Human();
156 child1.setBodyWeight( 5 );
157 child1.setDescription( "child1" );
158
159 Human child2 = new Human();
160 child2.setBodyWeight( 6 );
161 child2.setDescription( "child2" );
162
163 Human friend = new Human();
164 friend.setBodyWeight( 20 );
165 friend.setDescription( "friend" );
166
167 child1.setMother( mother );
168 child1.setFather( father );
169 mother.addOffspring( child1 );
170 father.addOffspring( child1 );
171
172 child2.setMother( mother );
173 child2.setFather( father );
174 mother.addOffspring( child2 );
175 father.addOffspring( child2 );
176
177 father.setFriends( new ArrayList() );
178 father.getFriends().add( friend );
179
180 session.save( mother );
181 session.save( father );
182 session.save( child1 );
183 session.save( child2 );
184 session.save( friend );
185
186 txn.commit();
187 session.close();
188 }
189
190 public void cleanup() {
191 Session session = openSession();
192 Transaction txn = session.beginTransaction();
193 session.createQuery( "delete Animal where mother is not null" ).executeUpdate();
194 List humansWithFriends = session.createQuery( "from Human h where exists(from h.friends)" ).list();
195 Iterator itr = humansWithFriends.iterator();
196 while ( itr.hasNext() ) {
197 session.delete( itr.next() );
198 }
199 session.createQuery( "delete Animal" ).executeUpdate();
200 txn.commit();
201 session.close();
202 }
203 }
204 }