HHH-12685 - Add tests
This commit is contained in:
parent
d64a9a9000
commit
673e4f7d01
|
@ -6,7 +6,17 @@
|
|||
*/
|
||||
package org.hibernate.test.criteria.hhh12685;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Parameter;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
@ -15,10 +25,10 @@ import javax.persistence.criteria.Predicate;
|
|||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.query.spi.QueryImplementor;
|
||||
import org.hibernate.type.StringType;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.test.criteria.Bid;
|
||||
import org.hibernate.test.criteria.Item;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
|
@ -28,7 +38,7 @@ public class CriteriaQueryIsBoundManualParameterTest extends BaseEntityManagerFu
|
|||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Bid.class, // Reuse to not create new
|
||||
Bid.class,
|
||||
Item.class
|
||||
};
|
||||
}
|
||||
|
@ -50,4 +60,131 @@ public class CriteriaQueryIsBoundManualParameterTest extends BaseEntityManagerFu
|
|||
} );
|
||||
}
|
||||
|
||||
@TestForIssue(jiraKey = "HHH-12685")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCriteriaQueryGetParameterOfWrongType() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> query = builder.createQuery( Item.class );
|
||||
Root<Item> root = query.from( Item.class );
|
||||
ParameterExpression<String> parameter = builder.parameter( String.class, "name" );
|
||||
Predicate predicate = builder.equal( root.get( "name" ), parameter );
|
||||
query.where( predicate );
|
||||
TypedQuery<Item> criteriaQuery = entityManager.createQuery( query );
|
||||
criteriaQuery.getParameter( "name", Integer.class );
|
||||
} );
|
||||
}
|
||||
|
||||
@TestForIssue(jiraKey = "HHH-12685")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCriteriaQueryGetNonExistingParameter() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> query = builder.createQuery( Item.class );
|
||||
Root<Item> root = query.from( Item.class );
|
||||
ParameterExpression<String> parameter = builder.parameter( String.class, "name" );
|
||||
Predicate predicate = builder.equal( root.get( "name" ), parameter );
|
||||
query.where( predicate );
|
||||
TypedQuery<Item> criteriaQuery = entityManager.createQuery( query );
|
||||
criteriaQuery.getParameter( "placedAt" );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetParameterPassingTypeNotFails() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery query = builder.createQuery( Item.class );
|
||||
|
||||
Predicate predicate = builder.equal(
|
||||
query.from( Item.class ).get( "name" ),
|
||||
builder.parameter( String.class, "name" )
|
||||
);
|
||||
query.where( predicate );
|
||||
|
||||
QueryImplementor criteriaQuery = (QueryImplementor) entityManager.createQuery( query );
|
||||
|
||||
criteriaQuery.setParameter( "name", "2", StringType.INSTANCE ).list();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetParameterTypeInstantNotFails() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery query = builder.createQuery( Bid.class );
|
||||
|
||||
Predicate predicate = builder.equal(
|
||||
query.from( Bid.class ).get( "placedAt" ),
|
||||
builder.parameter( Instant.class, "placedAt" )
|
||||
);
|
||||
query.where( predicate );
|
||||
|
||||
QueryImplementor criteriaQuery = (QueryImplementor) entityManager.createQuery( query );
|
||||
|
||||
criteriaQuery.setParameter( "placedAt", Instant.now(), TemporalType.TIMESTAMP ).list();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetParameterOfTypeInstantToAFloatParameterType() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery query = builder.createQuery( Bid.class );
|
||||
|
||||
Predicate predicate = builder.equal(
|
||||
query.from( Bid.class ).get( "amount" ),
|
||||
builder.parameter( Instant.class, "placedAt" )
|
||||
);
|
||||
query.where( predicate );
|
||||
|
||||
QueryImplementor criteriaQuery = (QueryImplementor) entityManager.createQuery( query );
|
||||
|
||||
criteriaQuery.setParameter( "placedAt", Instant.now(), TemporalType.TIMESTAMP ).list();
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetParameterOfTypeDateToAFloatParameterType() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery query = builder.createQuery( Bid.class );
|
||||
|
||||
Predicate predicate = builder.equal(
|
||||
query.from( Bid.class ).get( "amount" ),
|
||||
builder.parameter( Date.class, "placedAt" )
|
||||
);
|
||||
query.where( predicate );
|
||||
|
||||
QueryImplementor criteriaQuery = (QueryImplementor) entityManager.createQuery( query );
|
||||
|
||||
criteriaQuery.setParameter( "placedAt", Date.from(Instant.now()), TemporalType.DATE ).list();
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Bid implements Serializable {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
float amount;
|
||||
|
||||
Instant placedAt;
|
||||
|
||||
@Id
|
||||
@ManyToOne
|
||||
Item item;
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Item implements Serializable {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "item")
|
||||
Set<Bid> bids = new HashSet<>();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue