HHH-8283 JdbcSQLException with CompositeCustomType and java.util.Date

Conflicts:
	hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/QueryImpl.java
	hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/basic/BasicCriteriaUsageTest.java
This commit is contained in:
ouzned 2013-06-07 11:32:41 -04:00 committed by Brett Meyer
parent 80dc1e6fd1
commit adc6059a21
2 changed files with 43 additions and 10 deletions

View File

@ -58,6 +58,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.internal.QueryExecutionRequestException;
import org.hibernate.internal.SQLQueryImpl;
import org.hibernate.internal.AbstractQueryImpl;
import org.hibernate.type.CompositeCustomType;
import static javax.persistence.TemporalType.DATE;
import static javax.persistence.TemporalType.TIME;
@ -110,7 +111,7 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
final NamedParameterDescriptor descriptor =
queryImpl.getParameterMetadata().getNamedParameterDescriptor( name );
Class javaType = namedParameterTypeRedefinition.get( name );
if ( javaType != null && mightNeedRedefinition( javaType ) ) {
if ( javaType != null && mightNeedRedefinition( javaType, descriptor.getExpectedType().getClass() ) ) {
descriptor.resetExpectedType(
sfi().getTypeResolver().heuristicType( javaType.getName() )
);
@ -150,9 +151,10 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
return (SessionFactoryImplementor) getEntityManager().getFactory().getSessionFactory();
}
private boolean mightNeedRedefinition(Class javaType) {
// for now, only really no for dates/times/timestamps
return java.util.Date.class.isAssignableFrom( javaType );
private boolean mightNeedRedefinition(Class javaType, Class expectedType) {
// only redefine dates/times/timestamps that are not wrapped in a CompositeCustomType
return java.util.Date.class.isAssignableFrom( javaType )
&& !CompositeCustomType.class.isAssignableFrom( expectedType );
}
private static class ParameterImpl implements Parameter {

View File

@ -23,19 +23,25 @@
*/
package org.hibernate.ejb.criteria.basic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;
import org.junit.Test;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.hibernate.ejb.criteria.basic.Payment_;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/**
* @author Steve Ebersole
@ -43,7 +49,7 @@ import static org.junit.Assert.assertNotNull;
public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] { Wall.class };
return new Class[] { Wall.class, Payment.class };
}
@Test
@ -74,4 +80,29 @@ public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase
em.getTransaction().commit();
em.close();
}
@Test
@TestForIssue(jiraKey = "HHH-8283")
public void testDateCompositeCustomType() {
Payment payment = new Payment();
payment.setAmount( new BigDecimal( 1000 ) );
payment.setDate( new Date() );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( payment );
CriteriaQuery<Payment> criteria = em.getCriteriaBuilder().createQuery( Payment.class );
Root<Payment> rp = criteria.from( Payment.class );
Predicate predicate = em.getCriteriaBuilder().equal( rp.get( Payment_.date ), new Date() );
criteria.where( predicate );
TypedQuery<Payment> q = em.createQuery( criteria );
List<Payment> payments = q.getResultList();
assertEquals( 1, payments.size() );
em.getTransaction().commit();
em.close();
}
}