HHH-8373 fix and test case

This commit is contained in:
Brett Meyer 2013-08-01 12:02:35 -04:00
parent dd1bec53fb
commit 2301fd215f
2 changed files with 43 additions and 16 deletions

View File

@ -23,13 +23,10 @@
*/ */
package org.hibernate.jpa.internal; package org.hibernate.jpa.internal;
import javax.persistence.NoResultException; import static javax.persistence.TemporalType.DATE;
import javax.persistence.NonUniqueResultException; import static javax.persistence.TemporalType.TIME;
import javax.persistence.ParameterMode; import static javax.persistence.TemporalType.TIMESTAMP;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -39,7 +36,13 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.jboss.logging.Logger; import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
@ -59,10 +62,8 @@ import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.jpa.spi.AbstractEntityManagerImpl; import org.hibernate.jpa.spi.AbstractEntityManagerImpl;
import org.hibernate.jpa.spi.AbstractQueryImpl; import org.hibernate.jpa.spi.AbstractQueryImpl;
import org.hibernate.type.CompositeCustomType; import org.hibernate.type.CompositeCustomType;
import org.hibernate.type.Type;
import static javax.persistence.TemporalType.DATE; import org.jboss.logging.Logger;
import static javax.persistence.TemporalType.TIME;
import static javax.persistence.TemporalType.TIMESTAMP;
/** /**
* Hibernate implementation of both the {@link Query} and {@link TypedQuery} contracts. * Hibernate implementation of both the {@link Query} and {@link TypedQuery} contracts.
@ -103,7 +104,7 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
for ( String name : (Set<String>) parameterMetadata.getNamedParameterNames() ) { for ( String name : (Set<String>) parameterMetadata.getNamedParameterNames() ) {
final NamedParameterDescriptor descriptor = parameterMetadata.getNamedParameterDescriptor( name ); final NamedParameterDescriptor descriptor = parameterMetadata.getNamedParameterDescriptor( name );
Class javaType = namedParameterTypeRedefinition.get( name ); Class javaType = namedParameterTypeRedefinition.get( name );
if ( javaType != null && mightNeedRedefinition( javaType, descriptor.getExpectedType().getClass() ) ) { if ( javaType != null && mightNeedRedefinition( javaType, descriptor.getExpectedType() ) ) {
descriptor.resetExpectedType( descriptor.resetExpectedType(
sfi().getTypeResolver().heuristicType( javaType.getName() ) sfi().getTypeResolver().heuristicType( javaType.getName() )
); );
@ -136,10 +137,13 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
return (SessionFactoryImplementor) getEntityManager().getFactory().getSessionFactory(); return (SessionFactoryImplementor) getEntityManager().getFactory().getSessionFactory();
} }
private boolean mightNeedRedefinition(Class javaType, Class expectedType) { private boolean mightNeedRedefinition(Class javaType, Type expectedType) {
// only redefine dates/times/timestamps that are not wrapped in a CompositeCustomType // only redefine dates/times/timestamps that are not wrapped in a CompositeCustomType
if ( expectedType == null )
return java.util.Date.class.isAssignableFrom( javaType );
else
return java.util.Date.class.isAssignableFrom( javaType ) return java.util.Date.class.isAssignableFrom( javaType )
&& !CompositeCustomType.class.isAssignableFrom( expectedType ); && !CompositeCustomType.class.isAssignableFrom( expectedType.getClass() );
} }
private static class ParameterRegistrationImpl<T> implements ParameterRegistration<T> { private static class ParameterRegistrationImpl<T> implements ParameterRegistration<T> {

View File

@ -32,6 +32,7 @@ import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
@ -103,4 +104,26 @@ public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
} }
@Test
@TestForIssue(jiraKey = "HHH-8373")
public void testFunctionCriteria() {
Wall wall = new Wall();
wall.setColor( "yellow" );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( wall );
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where( cb.equal( root.get( "color" ), cb.lower( cb.literal( "YELLOW" ) ) ) );
Wall resultItem = em.createQuery( query ).getSingleResult();
assertNotNull( resultItem );
em.getTransaction().commit();
em.close();
}
} }