Skip to content

Instantly share code, notes, and snippets.

@dreab8
Created February 1, 2019 17:17
Show Gist options
  • Save dreab8/803f44b33f421db69ca4fbb551024556 to your computer and use it in GitHub Desktop.
Save dreab8/803f44b33f421db69ca4fbb551024556 to your computer and use it in GitHub Desktop.
Query with Tuple in where clause
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.query.hql;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Andrea Boriero
*/
public class TupleTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
TestEntity.class,
};
}
@Test
public void testEqualityPredicate() {
sessionFactoryScope().inTransaction(
session -> {
List results = session.createQuery(
"select o from TestEntity o where o.embeddable = (:e1, :e2)" )
.setParameter( "e1", 1 )
.setParameter( "e2", 2 )
.list();
assertThat( results.size(), is( 1 ) );
} );
}
@BeforeEach
public void setUp() {
sessionFactoryScope().inTransaction(
session -> {
TestEntity entity = new TestEntity(
1L,
new TestEmbeddable( 1, 2 )
);
session.save( entity );
TestEntity second_entity = new TestEntity(
2L,
new TestEmbeddable( 1, 3 )
);
session.save( entity );
session.save( second_entity );
} );
}
@AfterEach
public void tearDown() {
sessionFactoryScope().inTransaction(
session -> {
session.createQuery( "from TestEntity e" )
.list()
.forEach( entity -> session.delete( entity ) );
} );
}
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
private Long id;
@Embedded
private TestEmbeddable embeddable;
public TestEntity() {
}
public TestEntity(Long id, TestEmbeddable embeddable) {
this.id = id;
this.embeddable = embeddable;
}
}
@Embeddable
public static class TestEmbeddable {
private int firstField;
private int secondField;
public TestEmbeddable() {
}
public TestEmbeddable(int firstField, int secondField) {
this.firstField = firstField;
this.secondField = secondField;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment