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

This commit is contained in:
ouzned 2013-05-31 23:05:02 +02:00 committed by Brett Meyer
parent e2922ca5f5
commit d49f68f1e3
4 changed files with 244 additions and 15 deletions

View File

@ -58,6 +58,7 @@ import org.hibernate.jpa.internal.util.ConfigurationHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.jpa.spi.AbstractEntityManagerImpl;
import org.hibernate.jpa.spi.AbstractQueryImpl;
import org.hibernate.type.CompositeCustomType;
import static javax.persistence.TemporalType.DATE;
import static javax.persistence.TemporalType.TIME;
@ -102,7 +103,7 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
for ( String name : (Set<String>) parameterMetadata.getNamedParameterNames() ) {
final NamedParameterDescriptor descriptor = parameterMetadata.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() )
);
@ -135,13 +136,12 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
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 ParameterRegistrationImpl<T> implements ParameterRegistration<T> {
private final org.hibernate.Query query;

View File

@ -23,27 +23,33 @@
*/
package org.hibernate.jpa.test.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.jpa.test.BaseEntityManagerFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/**
* @author Steve Ebersole
*/
public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] { Wall.class };
return new Class[] { Wall.class, Payment.class };
}
@Test
@ -53,9 +59,7 @@ public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase
CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery( Wall.class );
Root<Wall> from = criteria.from( Wall.class );
ParameterExpression param = em.getCriteriaBuilder().parameter( String.class );
SingularAttribute<? super Wall,?> colorAttribute = em.getMetamodel()
.entity( Wall.class )
.getDeclaredSingularAttribute( "color" );
SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity( Wall.class ).getDeclaredSingularAttribute( "color" );
assertNotNull( "metamodel returned null singular attribute", colorAttribute );
Predicate predicate = em.getCriteriaBuilder().equal( from.get( colorAttribute ), param );
criteria.where( predicate );
@ -74,4 +78,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();
}
}

View File

@ -0,0 +1,145 @@
package org.hibernate.jpa.test.criteria.basic;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
/**
* @author Francois Gerodez
*/
public class Date3Type implements CompositeUserType {
@Override
public String[] getPropertyNames() {
return new String[] { "year", "month", "day" };
}
@Override
public Type[] getPropertyTypes() {
return new Type[] { StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER };
}
@Override
public Object getPropertyValue(Object component, int property) throws HibernateException {
Date date = (Date) component;
Calendar c = GregorianCalendar.getInstance();
c.setTime( date );
switch ( property ) {
case 0:
return c.get( Calendar.YEAR );
case 1:
return c.get( Calendar.MONTH );
case 2:
return c.get( Calendar.DAY_OF_MONTH );
}
throw new HibernateException( "Invalid property provided" );
}
@Override
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
Date date = (Date) component;
Calendar c = GregorianCalendar.getInstance();
c.setTime( date );
switch ( property ) {
case 0:
c.set( Calendar.YEAR, (Integer) value );
case 1:
c.set( Calendar.MONTH, (Integer) value );
case 2:
c.set( Calendar.DAY_OF_MONTH, (Integer) value );
default:
throw new HibernateException( "Invalid property provided" );
}
}
@Override
public Class returnedClass() {
return Date.class;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
if ( x == y )
return true;
if ( x == null || y == null )
return false;
Date dx = (Date) x;
Date dy = (Date) y;
return dx.equals( dy );
}
@Override
public int hashCode(Object x) throws HibernateException {
Date dx = (Date) x;
return dx.hashCode();
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
Date date = new Date();
Calendar c = GregorianCalendar.getInstance();
c.setTime( date );
Integer year = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[0], session );
Integer month = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[1], session );
Integer day = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[2], session );
c.set( year, month, day );
return date;
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
Date date = new Date();
Calendar c = GregorianCalendar.getInstance();
c.setTime( date );
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.YEAR ), index, session );
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.MONTH ), index + 1, session );
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.DAY_OF_MONTH ), index + 2, session );
}
@Override
public Object deepCopy(Object value) throws HibernateException {
if ( value == null )
return null;
Date date = (Date) value;
return date.clone();
}
@Override
public boolean isMutable() {
return true;
}
@Override
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
return (Serializable) deepCopy( value );
}
@Override
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
return deepCopy( cached );
}
@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
return deepCopy( original ); // TODO: improve
}
}

View File

@ -0,0 +1,55 @@
package org.hibernate.jpa.test.criteria.basic;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
/**
* @author Francois Gerodez
*/
@Entity
@Table(name = "crit_basic_payment")
@TypeDef(name = "paymentDate", typeClass = Date3Type.class)
public class Payment {
private Long id;
private BigDecimal amount;
private Date date;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
@Type(type = "paymentDate")
@Columns(columns = { @Column(name = "YEARPAYMENT"), @Column(name = "MONTHPAYMENT"), @Column(name = "DAYPAYMENT") })
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}