HHH-8172 : @Embedded object containing a null attribute cannot be used as a query parameter (test case)

This commit is contained in:
Gail Badner 2015-07-01 21:31:17 -07:00
parent e5f4b616d4
commit abd0461d34
1 changed files with 165 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import org.hibernate.Transaction;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.annotations.embedded.FloatLeg.RateIndex;
@ -29,6 +30,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
@ -74,6 +76,169 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8172" )
public void testQueryWithEmbeddedIsNull() throws Exception {
Session s;
Transaction tx;
Person p = new Person();
Address a = new Address();
Country c = new Country();
Country bornCountry = new Country();
c.setIso2( "DM" );
c.setName( "Matt Damon Land" );
assertNull( bornCountry.getIso2() );
assertNull( bornCountry.getName() );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Person) s.createQuery( "from Person p where p.bornIn is null" ).uniqueResult();
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertNull( p.bornIn );
tx.commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8172" )
@FailureExpected( jiraKey = "HHH-8172")
public void testQueryWithEmbeddedParameterAllNull() throws Exception {
Session s;
Transaction tx;
Person p = new Person();
Address a = new Address();
Country c = new Country();
Country bornCountry = new Country();
assertNull( bornCountry.getIso2() );
assertNull( bornCountry.getName() );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ).setParameter( "b", p.bornIn ).uniqueResult();
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertNull( p.bornIn );
tx.commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8172" )
@FailureExpected( jiraKey = "HHH-8172")
public void testQueryWithEmbeddedParameterOneNull() throws Exception {
Session s;
Transaction tx;
Person p = new Person();
Address a = new Address();
Country c = new Country();
Country bornCountry = new Country();
c.setIso2( "DM" );
c.setName( "Matt Damon Land" );
bornCountry.setIso2( "US" );
assertNull( bornCountry.getName() );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ).setParameter( "b", p.bornIn ).uniqueResult();
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertEquals( "US", p.bornIn.getIso2() );
assertNull( p.bornIn.getName() );
tx.commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8172" )
public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception {
Session s;
Transaction tx;
Person p = new Person();
Address a = new Address();
Country c = new Country();
Country bornCountry = new Country();
c.setIso2( "DM" );
c.setName( "Matt Damon Land" );
bornCountry.setIso2( "US" );
assertNull( bornCountry.getName() );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Person) s.createQuery( "from Person p " +
"where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " +
"( p.bornIn.name is null or p.bornIn.name = :n )"
).setParameter( "i", p.bornIn.getIso2() )
.setParameter( "n", p.bornIn.getName() )
.uniqueResult();
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertEquals( "US", p.bornIn.getIso2() );
assertNull( p.bornIn.getName() );
tx.commit();
s.close();
}
@Test
public void testCompositeId() throws Exception {
Session s;