Skip to content

Commit abd0461

Browse files
committed
HHH-8172 : @Embedded object containing a null attribute cannot be used as a query parameter (test case)
1 parent e5f4b61 commit abd0461

File tree

1 file changed

+165
-0
lines changed
  • hibernate-core/src/test/java/org/hibernate/test/annotations/embedded

1 file changed

+165
-0
lines changed

hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hibernate.boot.MetadataBuilder;
2020
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
2121

22+
import org.hibernate.testing.FailureExpected;
2223
import org.hibernate.testing.TestForIssue;
2324
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
2425
import org.hibernate.test.annotations.embedded.FloatLeg.RateIndex;
@@ -29,6 +30,7 @@
2930
import static org.junit.Assert.assertEquals;
3031
import static org.junit.Assert.assertFalse;
3132
import static org.junit.Assert.assertNotNull;
33+
import static org.junit.Assert.assertNull;
3234
import static org.junit.Assert.assertTrue;
3335

3436
/**
@@ -74,6 +76,169 @@ public void testSimple() throws Exception {
7476
s.close();
7577
}
7678

79+
@Test
80+
@TestForIssue( jiraKey = "HHH-8172" )
81+
public void testQueryWithEmbeddedIsNull() throws Exception {
82+
Session s;
83+
Transaction tx;
84+
Person p = new Person();
85+
Address a = new Address();
86+
Country c = new Country();
87+
Country bornCountry = new Country();
88+
c.setIso2( "DM" );
89+
c.setName( "Matt Damon Land" );
90+
assertNull( bornCountry.getIso2() );
91+
assertNull( bornCountry.getName() );
92+
93+
a.address1 = "colorado street";
94+
a.city = "Springfield";
95+
a.country = c;
96+
p.address = a;
97+
p.bornIn = bornCountry;
98+
p.name = "Homer";
99+
s = openSession();
100+
tx = s.beginTransaction();
101+
s.persist( p );
102+
tx.commit();
103+
s.close();
104+
105+
s = openSession();
106+
tx = s.beginTransaction();
107+
p = (Person) s.createQuery( "from Person p where p.bornIn is null" ).uniqueResult();
108+
assertNotNull( p );
109+
assertNotNull( p.address );
110+
assertEquals( "Springfield", p.address.city );
111+
assertNotNull( p.address.country );
112+
assertEquals( "DM", p.address.country.getIso2() );
113+
assertNull( p.bornIn );
114+
tx.commit();
115+
s.close();
116+
}
117+
118+
@Test
119+
@TestForIssue( jiraKey = "HHH-8172" )
120+
@FailureExpected( jiraKey = "HHH-8172")
121+
public void testQueryWithEmbeddedParameterAllNull() throws Exception {
122+
Session s;
123+
Transaction tx;
124+
Person p = new Person();
125+
Address a = new Address();
126+
Country c = new Country();
127+
Country bornCountry = new Country();
128+
assertNull( bornCountry.getIso2() );
129+
assertNull( bornCountry.getName() );
130+
131+
a.address1 = "colorado street";
132+
a.city = "Springfield";
133+
a.country = c;
134+
p.address = a;
135+
p.bornIn = bornCountry;
136+
p.name = "Homer";
137+
s = openSession();
138+
tx = s.beginTransaction();
139+
s.persist( p );
140+
tx.commit();
141+
s.close();
142+
143+
s = openSession();
144+
tx = s.beginTransaction();
145+
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ).setParameter( "b", p.bornIn ).uniqueResult();
146+
assertNotNull( p );
147+
assertNotNull( p.address );
148+
assertEquals( "Springfield", p.address.city );
149+
assertNotNull( p.address.country );
150+
assertEquals( "DM", p.address.country.getIso2() );
151+
assertNull( p.bornIn );
152+
tx.commit();
153+
s.close();
154+
}
155+
156+
@Test
157+
@TestForIssue( jiraKey = "HHH-8172" )
158+
@FailureExpected( jiraKey = "HHH-8172")
159+
public void testQueryWithEmbeddedParameterOneNull() throws Exception {
160+
Session s;
161+
Transaction tx;
162+
Person p = new Person();
163+
Address a = new Address();
164+
Country c = new Country();
165+
Country bornCountry = new Country();
166+
c.setIso2( "DM" );
167+
c.setName( "Matt Damon Land" );
168+
bornCountry.setIso2( "US" );
169+
assertNull( bornCountry.getName() );
170+
171+
a.address1 = "colorado street";
172+
a.city = "Springfield";
173+
a.country = c;
174+
p.address = a;
175+
p.bornIn = bornCountry;
176+
p.name = "Homer";
177+
s = openSession();
178+
tx = s.beginTransaction();
179+
s.persist( p );
180+
tx.commit();
181+
s.close();
182+
183+
s = openSession();
184+
tx = s.beginTransaction();
185+
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ).setParameter( "b", p.bornIn ).uniqueResult();
186+
assertNotNull( p );
187+
assertNotNull( p.address );
188+
assertEquals( "Springfield", p.address.city );
189+
assertNotNull( p.address.country );
190+
assertEquals( "DM", p.address.country.getIso2() );
191+
assertEquals( "US", p.bornIn.getIso2() );
192+
assertNull( p.bornIn.getName() );
193+
tx.commit();
194+
s.close();
195+
}
196+
197+
@Test
198+
@TestForIssue( jiraKey = "HHH-8172" )
199+
public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception {
200+
Session s;
201+
Transaction tx;
202+
Person p = new Person();
203+
Address a = new Address();
204+
Country c = new Country();
205+
Country bornCountry = new Country();
206+
c.setIso2( "DM" );
207+
c.setName( "Matt Damon Land" );
208+
bornCountry.setIso2( "US" );
209+
assertNull( bornCountry.getName() );
210+
211+
a.address1 = "colorado street";
212+
a.city = "Springfield";
213+
a.country = c;
214+
p.address = a;
215+
p.bornIn = bornCountry;
216+
p.name = "Homer";
217+
s = openSession();
218+
tx = s.beginTransaction();
219+
s.persist( p );
220+
tx.commit();
221+
s.close();
222+
223+
s = openSession();
224+
tx = s.beginTransaction();
225+
p = (Person) s.createQuery( "from Person p " +
226+
"where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " +
227+
"( p.bornIn.name is null or p.bornIn.name = :n )"
228+
).setParameter( "i", p.bornIn.getIso2() )
229+
.setParameter( "n", p.bornIn.getName() )
230+
.uniqueResult();
231+
assertNotNull( p );
232+
assertNotNull( p.address );
233+
assertEquals( "Springfield", p.address.city );
234+
assertNotNull( p.address.country );
235+
assertEquals( "DM", p.address.country.getIso2() );
236+
assertEquals( "US", p.bornIn.getIso2() );
237+
assertNull( p.bornIn.getName() );
238+
tx.commit();
239+
s.close();
240+
}
241+
77242
@Test
78243
public void testCompositeId() throws Exception {
79244
Session s;

0 commit comments

Comments
 (0)