HHH-4590 - CASTs from CriteriaBuilder.toXXX methods still need to be fleshed out

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18230 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-12-15 18:09:53 +00:00
parent dfe61a4a5e
commit 5a6686810b
2 changed files with 30 additions and 3 deletions

View File

@ -39,6 +39,9 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import org.hibernate.ejb.HibernateEntityManagerImplementor;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;
import org.hibernate.type.TypeFactory;
import org.hibernate.util.StringHelper;
/**
@ -61,6 +64,8 @@ public class CriteriaQueryCompiler {
public void registerExplicitParameter(ParameterExpression<?> criteriaQueryParameter, String jpaqlParameterName);
public void registerImplicitParameterBinding(ImplicitParameterBinding binding);
public String getCastType(Class javaType);
}
public static interface RenderedCriteriaQuery {
@ -106,6 +111,25 @@ public class CriteriaQueryCompiler {
public void registerImplicitParameterBinding(ImplicitParameterBinding binding) {
implicitParameterBindings.add( binding );
}
public String getCastType(Class javaType) {
SessionFactoryImplementor factory =
( SessionFactoryImplementor ) entityManager.getFactory().getSessionFactory();
Type hibernateType = TypeFactory.heuristicType( javaType.getName() );
if ( hibernateType == null ) {
throw new IllegalArgumentException(
"Could not convert java type [" + javaType.getName() + "] to Hibernate type"
);
}
int[] sqlTypeCodes = hibernateType.sqlTypes( factory );
if ( sqlTypeCodes.length != 1 ) {
throw new IllegalArgumentException(
"Invalid Hibernate Type [" + hibernateType.getName() +
"] for cast : more than one column spanned"
);
}
return factory.getDialect().getCastTypeName( sqlTypeCodes[0] );
}
};
RenderedCriteriaQuery renderedCriteriaQuery = criteriaQueryImpl.render( renderingContext );

View File

@ -58,9 +58,12 @@ public class CastFunction<T,Y> extends BasicFunctionExpression<T> implements Fun
Helper.possibleParameter( getCastSource(), registry );
}
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
// HHH-4590
// todo : how to handle these, espeically if in the select...
return super.render( renderingContext );
return CAST_NAME + '(' +
castSource.render( renderingContext ) +
" as " +
renderingContext.getCastType( getJavaType() ) +
')';
}
}