-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-15454 create test where metamodel for primitive type field is use…
…d in tuple query
- Loading branch information
1 parent
067d357
commit 4b41584
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...nate-core/src/test/java/org/hibernate/jpa/test/query/TupleQueryRetrievePrimitiveTest.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,85 @@ | ||
package org.hibernate.jpa.test.query; | ||
|
||
import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest; | ||
import org.hibernate.jpa.test.metamodel.ThingWithQuantity; | ||
import org.hibernate.jpa.test.metamodel.ThingWithQuantity_; | ||
import org.hibernate.testing.TestForIssue; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.Tuple; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Path; | ||
import javax.persistence.criteria.Root; | ||
|
||
@TestForIssue( jiraKey = "HHH-15454" ) | ||
public class TupleQueryRetrievePrimitiveTest extends AbstractMetamodelSpecificTest { | ||
|
||
public static final int QUANTITY_OF_THING = 3; | ||
private EntityManager em; | ||
|
||
|
||
@Before | ||
public void createThingWithQuantity() { | ||
em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
|
||
ThingWithQuantity thing = new ThingWithQuantity(); | ||
thing.setId( "thingWithQuantity3" ); | ||
thing.setName( "3 Things" ); | ||
thing.setQuantity(QUANTITY_OF_THING); | ||
em.persist( thing ); | ||
|
||
em.getTransaction().commit(); | ||
} | ||
|
||
@After | ||
public void endEntityManager() { | ||
em.close(); | ||
} | ||
|
||
@Test | ||
public void testRetrieveTupleEntryWithPrimitiveType() { | ||
final Tuple result = queryTuple(); | ||
final int quantity = result.get(ThingWithQuantity_.quantity.getName(), int.class); | ||
Assert.assertEquals(QUANTITY_OF_THING, quantity); | ||
} | ||
|
||
@Test | ||
public void testRetrieveTupleEntryWithMetadata() { | ||
final Tuple result = queryTuple(); | ||
final int quantity = result.get(ThingWithQuantity_.quantity.getName(), ThingWithQuantity_.quantity.getJavaType()); | ||
Assert.assertEquals(QUANTITY_OF_THING, quantity); | ||
} | ||
|
||
@Test | ||
public void testRetrieveTupleEntryFromIndex() { | ||
final Tuple result = queryTuple(); | ||
final int quantity = result.get(0, ThingWithQuantity_.quantity.getJavaType()); | ||
Assert.assertEquals(QUANTITY_OF_THING, quantity); | ||
} | ||
|
||
@Test | ||
public void testRetrieveTupleEntryWithTupleElement() { | ||
final CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
final CriteriaQuery<Tuple> query = cb.createTupleQuery(); | ||
final Root<ThingWithQuantity> thingWithQuantity = query.from(ThingWithQuantity.class); | ||
final Path<Integer> tupleElement = thingWithQuantity.get(ThingWithQuantity_.quantity); | ||
query.multiselect(tupleElement.alias(ThingWithQuantity_.quantity.getName())); | ||
Tuple result = em.createQuery(query).setMaxResults(1).getSingleResult(); | ||
final int quantity = result.get(tupleElement); | ||
Assert.assertEquals(QUANTITY_OF_THING, quantity); | ||
} | ||
|
||
private Tuple queryTuple() { | ||
final CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
final CriteriaQuery<Tuple> query = cb.createTupleQuery(); | ||
final Root<ThingWithQuantity> thingWithQuantity = query.from(ThingWithQuantity.class); | ||
query.multiselect(thingWithQuantity.get(ThingWithQuantity_.quantity).alias(ThingWithQuantity_.quantity.getName())); | ||
return em.createQuery(query).setMaxResults(1).getSingleResult(); | ||
} | ||
} |