Skip to content

Commit

Permalink
HHH-10577 - Added additional test cases for VALUE()
Browse files Browse the repository at this point in the history
(cherry picked from commit fa31715)

Conflicts:
	hibernate-core/src/test/java/org/hibernate/test/hql/CollectionMapWithComponentValueTest.java
  • Loading branch information
Naros authored and gbadner committed Feb 21, 2017
1 parent afee667 commit a4974a4
Showing 1 changed file with 56 additions and 10 deletions.
Expand Up @@ -12,16 +12,15 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.resource.transaction.spi.TransactionStatus;

import org.hibernate.HibernateException;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Test;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
Expand All @@ -30,13 +29,15 @@
import static org.hamcrest.core.Is.is;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* @author Andrea Boriero
* @author Christian Beikov
*/
public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestCase {
private final KeyValue keyValue = new KeyValue( "key1" );
private final EmbeddableValue embeddableValue = new EmbeddableValue( 3 );

@Override
protected Class<?>[] getAnnotatedClasses() {
Expand All @@ -54,8 +55,6 @@ protected void prepareTest() throws Exception {
s.save( keyValue );

TestEntity testEntity = new TestEntity();
EmbeddableValue embeddableValue = new EmbeddableValue();
embeddableValue.value = 3;
Map<KeyValue, EmbeddableValue> map = new HashMap<>();
map.put( keyValue, embeddableValue );
testEntity.values = map;
Expand All @@ -64,10 +63,8 @@ protected void prepareTest() throws Exception {
KeyValue keyValue2 = new KeyValue( "key2" );
s.save( keyValue2 );
TestEntity testEntity2 = new TestEntity();
EmbeddableValue embeddableValue2 = new EmbeddableValue();
embeddableValue2.value = 3;
Map<KeyValue, EmbeddableValue> map2 = new HashMap<>();
map.put( keyValue2, embeddableValue2 );
map.put( keyValue2, embeddableValue );
testEntity2.values = map2;
s.save( testEntity2 );
}
Expand All @@ -81,8 +78,7 @@ public void testMapKeyExpressionInWhere() {
s.getTransaction().begin();
{
// JPA form
Query query = s.createQuery(
"select te from TestEntity te join te.values v where ? in (key(v)) " );
Query query = s.createQuery( "select te from TestEntity te join te.values v where ? in (key(v)) " );
query.setParameter( 0, keyValue );

assertThat( query.list().size(), is( 1 ) );
Expand All @@ -92,6 +88,48 @@ public void testMapKeyExpressionInWhere() {
query.setParameter( 0, keyValue );

assertThat( query.list().size(), is( 1 ) );

// Test key property dereference
query = s.createQuery( "select te from TestEntity te join te.values v where key(v).name in :names" );
query.setParameterList( "names", Arrays.asList( keyValue.name ) );
assertThat( query.list().size(), is( 1 ) );
}
s.getTransaction().commit();
s.close();
}

@Test
@TestForIssue(jiraKey = "HHH-10577")
public void testMapValueExpressionInWhere() {
Session s = openSession();
s.getTransaction().begin();
{
// JPA form
try {
Query query = s.createQuery( "select te from TestEntity te join te.values v where ? in (value(v))" );
query.setParameter( 0, new EmbeddableValue( 3 ) );
assertThat( query.list().size(), is( 2 ) );
fail( "HibernateException expected - Could not determine type for EmbeddableValue" );
}
catch ( Exception e ) {
assertTyping( HibernateException.class, e );
}

// Hibernate additional form
try {
Query query = s.createQuery( "select te from TestEntity te where ? in (value(te.values))" );
query.setParameter( 0, new EmbeddableValue( 3 ) );
assertThat( query.list().size(), is( 2 ) );
fail( "HibernateException expected - Could not determine type for EmbeddableValue" );
}
catch ( Exception e ) {
assertTyping( HibernateException.class, e );
}

// Test value property dereference
Query query = s.createQuery( "select te from TestEntity te join te.values v where value(v).value in :values" );
query.setParameterList( "values", Arrays.asList( 3 ) );
assertThat( query.list().size(), is( 2 ) );
}
s.getTransaction().commit();
s.close();
Expand Down Expand Up @@ -201,5 +239,13 @@ public KeyValue(String name) {
@Embeddable
public static class EmbeddableValue {
Integer value;

EmbeddableValue() {

}

EmbeddableValue(Integer value) {
this.value = value;
}
}
}

0 comments on commit a4974a4

Please sign in to comment.