[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5755) javax.persistence.criteria.Expression.as() is broken

Paul Hodchenkov (JIRA) noreply at atlassian.com
Mon Nov 21 02:04:20 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5755?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44366#comment-44366 ] 

Paul Hodchenkov commented on HHH-5755:
--------------------------------------

I also have a problem with builder.like and expression.as(String.class) in hibernate 4.0.0.CR6 after migrating from eclipselink.
Query fails with:
java.lang.IllegalArgumentException: Parameter value [%2%] was not matching type [java.lang.Character]
For some reason hibernate casts integer to java.lang.Character instead of String.
To workaround this i have added custom SQL function to dialect:
    public static class CastToStringFunction implements SQLFunction {
        @Override
        public boolean hasArguments() {
            return true;
        }

        @Override
        public boolean hasParenthesesIfNoArguments() {
            return false;
        }

        @Override
        public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
            return StandardBasicTypes.STRING;
        }

        @Override
        public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) throws QueryException {
            return "cast(" + arguments.get(0) + " as char)";
        }
    }
So i use builder.function(CAST_TO_STRING_FUNCTION, String.class, field) instead of field.as(String.class)

> javax.persistence.criteria.Expression.as() is broken
> ----------------------------------------------------
>
>                 Key: HHH-5755
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5755
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: entity-manager
>    Affects Versions: 3.6.0
>            Reporter: Artem Troitskiy
>            Priority: Minor
>         Attachments: HHH-5755.patch
>
>
> When I try to execute criteria query containing typecast to {{String}}
> {noformat}
> CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
> Root<Product> from = criteria.from( Product.class );
> criteria.where(builder.like(from.get("partNumber").as(String.class), "1%"));
> List<Product> result = em.createQuery( criteria ).getResultList();
> {noformat}
> I get the following exception
> {quote}
> org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 132 [select generatedAlias0 from org.hibernate.ejb.metamodel.Product as generatedAlias0 where cast(generatedAlias0.partNumber as varchar(255)) like :param0]
> {quote}
> As you can see, SQL type is used in HQL {{cast}} clause. It's caused by {{CriteriaQueryCompiler.RenderingContext.getCastType()}} that erroneously return SQL typename instead of Hibernate typename.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list