Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
350 additions
and
9 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
...ate-core/src/test/java/org/hibernate/orm/test/query/results/BasicCriteriaResultTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* 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.results; | ||
|
||
import java.util.List; | ||
import javax.persistence.Tuple; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Path; | ||
import javax.persistence.criteria.Root; | ||
import javax.persistence.metamodel.SingularAttribute; | ||
|
||
import org.hibernate.metamodel.model.domain.EntityDomainType; | ||
import org.hibernate.metamodel.model.domain.JpaMetamodel; | ||
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@DomainModel( annotatedClasses = SimpleEntity.class ) | ||
@SessionFactory | ||
public class BasicCriteriaResultTests { | ||
@BeforeEach | ||
public void prepareTestData(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
session.save( new SimpleEntity( 1, "first", new SimpleComposite( "a", "b" ) ) ); | ||
}); | ||
} | ||
|
||
@AfterEach | ||
public void dropTestData(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
session.createQuery( "delete SimpleEntity" ).executeUpdate(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); | ||
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); | ||
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity( SimpleEntity.class ); | ||
final SingularPersistentAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId( Integer.class ); | ||
|
||
final CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class ); | ||
final Root<SimpleEntity> root = criteria.from( SimpleEntity.class ); | ||
|
||
// final Path<Integer> idPath = root.get( SimpleEntity_ ); | ||
final Path<Integer> idPath = root.get( idAttribute ); | ||
criteria.select( idPath ); | ||
criteria.orderBy( builder.asc( idPath ) ); | ||
|
||
session.createQuery( criteria ).list(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicTupleSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); | ||
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); | ||
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity( SimpleEntity.class ); | ||
final SingularAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId( Integer.class ); | ||
final SingularAttribute<? super SimpleEntity, String> nameAttribute = entityDescriptor.getSingularAttribute( "name", String.class ); | ||
|
||
final CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class ); | ||
final Root<SimpleEntity> root = criteria.from( SimpleEntity.class ); | ||
|
||
final Path<Integer> idPath = root.get( idAttribute ); | ||
final Path<String> namePath = root.get( nameAttribute ); | ||
criteria.multiselect( idPath.alias( "id" ), namePath.alias( "name" ) ); | ||
criteria.orderBy( builder.asc( idPath ), builder.asc( namePath ) ); | ||
|
||
final List<Tuple> list = session.createQuery( criteria ).list(); | ||
assertThat( list ).hasSize( 1 ); | ||
|
||
final Tuple result = list.get( 0 ); | ||
assertThat( result.get( 0 ) ).isEqualTo( 1 ); | ||
assertThat( result.get( "id" ) ).isEqualTo( 1 ); | ||
|
||
assertThat( result.get( 1 ) ).isEqualTo( "first" ); | ||
assertThat( result.get( "name" ) ).isEqualTo( "first" ); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCompositeSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); | ||
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); | ||
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity( SimpleEntity.class ); | ||
final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute; | ||
compositeAttribute = entityDescriptor.getSingularAttribute( "composite", SimpleComposite.class ); | ||
|
||
final CriteriaQuery<SimpleComposite> criteria = builder.createQuery( SimpleComposite.class ); | ||
final Root<SimpleEntity> root = criteria.from( SimpleEntity.class ); | ||
|
||
final Path<SimpleComposite> attributePath = root.get( compositeAttribute ); | ||
criteria.select( attributePath ); | ||
criteria.orderBy( builder.asc( attributePath ) ); | ||
|
||
session.createQuery( criteria ).list(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicAndCompositeTuple(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); | ||
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); | ||
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity( SimpleEntity.class ); | ||
final SingularAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId( Integer.class ); | ||
final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute = entityDescriptor.getSingularAttribute( "composite", SimpleComposite.class ); | ||
|
||
final CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class ); | ||
final Root<SimpleEntity> root = criteria.from( SimpleEntity.class ); | ||
|
||
final Path<Integer> idPath = root.get( idAttribute ); | ||
final Path<SimpleComposite> compositePath = root.get( compositeAttribute ); | ||
criteria.multiselect( idPath.alias( "id" ), compositePath.alias( "composite" ) ); | ||
criteria.orderBy( builder.asc( idPath ) ); | ||
|
||
final List<Tuple> list = session.createQuery( criteria ).list(); | ||
assertThat( list ).hasSize( 1 ); | ||
|
||
final Tuple result = list.get( 0 ); | ||
assertThat( result.get( 0 ) ).isEqualTo( 1 ); | ||
assertThat( result.get( "id" ) ).isEqualTo( 1 ); | ||
assertThat( result.get( 0 ) ).isSameAs( result.get( "id" ) ); | ||
|
||
assertThat( result.get( 1 ) ).isInstanceOf( SimpleComposite.class ); | ||
assertThat( result.get( 1 ) ).isSameAs( result.get( "composite" ) ); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicAndCompositeArray(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); | ||
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); | ||
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity( SimpleEntity.class ); | ||
final SingularAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId( Integer.class ); | ||
final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute = entityDescriptor.getSingularAttribute( "composite", SimpleComposite.class ); | ||
|
||
final CriteriaQuery<Object[]> criteria = builder.createQuery( Object[].class ); | ||
final Root<SimpleEntity> root = criteria.from( SimpleEntity.class ); | ||
|
||
final Path<Integer> idPath = root.get( idAttribute ); | ||
final Path<SimpleComposite> compositePath = root.get( compositeAttribute ); | ||
criteria.multiselect( idPath.alias( "id" ), compositePath.alias( "composite" ) ); | ||
criteria.orderBy( builder.asc( idPath ) ); | ||
|
||
final List<Object[]> list = session.createQuery( criteria ).list(); | ||
assertThat( list ).hasSize( 1 ); | ||
|
||
final Object[] result = list.get( 0 ); | ||
assertThat( result[0] ).isEqualTo( 1 ); | ||
assertThat( result[1] ).isInstanceOf( SimpleComposite.class ); | ||
}); | ||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
hibernate-core/src/test/java/org/hibernate/orm/test/query/results/BasicHqlResultTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.results; | ||
|
||
import java.util.List; | ||
import javax.persistence.Tuple; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@DomainModel( annotatedClasses = SimpleEntity.class ) | ||
@SessionFactory | ||
public class BasicHqlResultTests { | ||
@BeforeEach | ||
public void prepareTestData(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
session.save( new SimpleEntity( 1, "first", new SimpleComposite( "a", "b" ) ) ); | ||
}); | ||
} | ||
|
||
@AfterEach | ||
public void dropTestData(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
session.createQuery( "delete SimpleEntity" ).executeUpdate(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
session.createQuery( "select id from SimpleEntity order by id", Integer.class ).list(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicTupleSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final List<Tuple> tuples = session | ||
.createQuery( "select id as id, name as name from SimpleEntity order by id, name", Tuple.class ) | ||
.list(); | ||
assertThat( tuples ).hasSize( 1 ); | ||
|
||
final Tuple result = tuples.get( 0 ); | ||
assertThat( result.get( 0 ) ).isEqualTo( 1 ); | ||
assertThat( result.get( "id" ) ).isEqualTo( 1 ); | ||
assertThat( result.get( 1 ) ).isEqualTo( "first" ); | ||
assertThat( result.get( "name" ) ).isEqualTo( "first" ); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCompositeSelection(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final List<SimpleComposite> list = session | ||
.createQuery( "select composite from SimpleEntity order by composite", SimpleComposite.class ) | ||
.list(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicAndCompositeTuple(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final String qry = "select id as id, composite as composite from SimpleEntity order by name"; | ||
final List<Tuple> list = session.createQuery( qry, Tuple.class ).list(); | ||
assertThat( list ).hasSize( 1 ); | ||
|
||
final Tuple result = list.get( 0 ); | ||
assertThat( result.get( 0 ) ).isEqualTo( 1 ); | ||
assertThat( result.get( "id" ) ).isEqualTo( 1 ); | ||
assertThat( result.get( 0 ) ).isSameAs( result.get( "id" ) ); | ||
|
||
assertThat( result.get( 1 ) ).isInstanceOf( SimpleComposite.class ); | ||
assertThat( result.get( 1 ) ).isSameAs( result.get( "composite" ) ); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testBasicAndCompositeArray(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final String qry = "select id, composite from SimpleEntity order by name"; | ||
final List<Object[]> list = session.createQuery( qry ).list(); | ||
assertThat( list ).hasSize( 1 ); | ||
|
||
final Object[] result = list.get( 0 ); | ||
assertThat( result[0] ).isEqualTo( 1 ); | ||
assertThat( result[1] ).isInstanceOf( SimpleComposite.class ); | ||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
hibernate-core/src/test/java/org/hibernate/orm/test/query/results/SimpleComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.results; | ||
|
||
import javax.persistence.Embeddable; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@Embeddable | ||
public class SimpleComposite { | ||
public String value1; | ||
public String value2; | ||
|
||
public SimpleComposite() { | ||
} | ||
|
||
public SimpleComposite(String value1, String value2) { | ||
this.value1 = value1; | ||
this.value2 = value2; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
hibernate-core/src/test/java/org/hibernate/orm/test/query/results/SimpleEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.results; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@Entity(name = "SimpleEntity") | ||
@Table(name = "simple_entity") | ||
public class SimpleEntity { | ||
@Id | ||
public Integer id; | ||
public String name; | ||
public SimpleComposite composite; | ||
|
||
public SimpleEntity() { | ||
} | ||
|
||
public SimpleEntity(Integer id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public SimpleEntity(Integer id, String name, SimpleComposite composite) { | ||
this.id = id; | ||
this.name = name; | ||
this.composite = composite; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters