-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Restrictions.like, could it be an issue?
PostPosted: Thu Feb 28, 2008 12:08 pm 
Newbie

Joined: Thu Feb 28, 2008 11:31 am
Posts: 2
Hi all,

I'm having a problem using the Criteria API, and I don't know if this is an usual behaviour, or on the contrary, it's an issue.

This is the code:

// Criteria
Criteria criteria = session.createCriteria(Bill.class);
criteria.createAlias("customer", "customer", CriteriaSpecification.LEFT_JOIN);

// Restrictions
criteria.add(Restrictions.like("billNumber", bill.getNumber().toString(), MatchMode.END));

// Result
return (List<Bill>) criteria.list();


The point is bill.getNumber() returns a Long object, so I need to toString it because of the prototype of the method. When I do the criteria.list() I get a ClassCastException because in the PreparedStatement Hibernate is trying to cast the String I supplied to a Long (property type). Obviously it cant be done, so the exception is thrown. Therefore you can only use this method with String properties.

I've tested it with 2 diferent versions: 3.2.6.ga and 3.2.4.ga, with the same result.

Do you think this is a normal behaviour or an issue?

Thanks in advance,
david


Hibernate version: 3.2.6.ga/3.2.4.ga
Mappings: JPA Annotations
Name and version of the database you are using: Oracle 10g
Full stack trace of any exception that occurs: java.lang.ClassCastException: java.lang.String
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1707)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1678)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.my.app.dao.jpa.BillingDaoJpa.getBillByCriteria(BillingDaoJpa.java:244)
at com.my.app.service.impl.BillingManagerImpl.getBillByCriteria(BillManagerImpl.java:226)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy150.getBillByCriteria(Unknown Source)
at com.my.app.webapp.service.impl.BillServiceImpl.getBillByBillNumber(BillServiceImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.directwebremoting.impl.ExecuteAjaxFilter.doFilter(ExecuteAjaxFilter.java:34)
at org.directwebremoting.impl.DefaultRemoter$1.doFilter(DefaultRemoter.java:428)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:431)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:283)
at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
at org.directwebremoting.spring.DwrSpringServlet.doPost(DwrSpringServlet.java:158)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.appfuse.webapp.filter.StaticFilter.doFilterInternal(StaticFilter.java:102)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:350)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.appfuse.webapp.filter.LocaleFilter.doFilterInternal(LocaleFilter.java:63)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:111)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 12:17 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
You can compare only String typed properties using MatchMode in Restrictions.like() method.

// Restrictions
Code:
criteria.add(Restrictions.like("billNumber", bill.getNumber().toString(), MatchMode.END));


Now you have two options:

a. Make billNumber property from Long to String type to use it with MatchMode. Then above restriction in query works fine.

b. Use Restrictions.like(String, Object) method with out MatchMode.

Why do you want to compare a bill.getNumber()-->Long value using MatchMode here.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 12:25 pm 
Newbie

Joined: Thu Feb 28, 2008 11:31 am
Posts: 2
Hi sjhyam, thanks for your answer. I'm using this to do an Autocompletion field in a html page, so I need to get all the bills whose number starts with the value I supply.

I've not tested it, but I'm not sure about the results I could get using Restrictions.like(String, Object) method, I mean, you are going to use a 'like' operator, so which MatchMode use it? If you don't include a '%' character in the criteria it would use a '=', I guess.

david


Top
 Profile  
 
 Post subject: Any luck?
PostPosted: Sat Jan 17, 2009 3:45 am 
Newbie

Joined: Sat Jan 17, 2009 3:31 am
Posts: 2
Hi,

Any luck with this issue?
I'm currently in a similar situation, in which I need a way to do a Restriction.ilike with a MatchMode.ANYWHERE for a Long property. The error that you're getting is the exact one that I'm getting.
How did you go about resolving this issue?

_________________
Maximos


Top
 Profile  
 
 Post subject: Re: Restrictions.like, could it be an issue?
PostPosted: Wed Aug 11, 2010 2:11 pm 
Newbie

Joined: Thu Apr 22, 2010 1:46 pm
Posts: 2
Thread necromancy! (Since this seems like an open question for the previous poster, and it was an open question for me.)

Code:
    private class TypelessLikeExpression implements Criterion {

        private String propertyName;
        private String value;

        public TypelessLikeExpression(String propertyName, String value) {
            this.propertyName = propertyName;
            this.value = value;
        }

        public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            Dialect dialect = criteriaQuery.getFactory().getDialect();
            String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
            if (columns.length != 1) {
                throw new HibernateException("Like may only be used with single-column properties");
            }
            CastFunction cast = (CastFunction) dialect.getFunctions().get("cast");
            String lhs = dialect.getLowercaseFunction() + '(' + cast.render(Arrays.asList(columns[0], "java.lang.String"), criteriaQuery.getFactory()) + ')';
            return lhs + " like ?";

        }

        public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new TypedValue[] { new TypedValue(new org.hibernate.type.StringType(), MatchMode.START.toMatchString(value.toLowerCase()), EntityMode.POJO) };
        }

    }


This one obviously has MatchMode.START behavior hardcoded in to it, as well as case insensitivity. Those should be fairly straightforward to generalize though... do it the same way the original LikeExpression does it.


Top
 Profile  
 
 Post subject: Re: Restrictions.like, could it be an issue?
PostPosted: Fri Nov 16, 2012 4:44 am 
Newbie

Joined: Fri Nov 16, 2012 4:14 am
Posts: 1
Hi all,

I had similar problem as i have mapped database LONG field as it is in the entity class.
I used Hibernate like operator and It gave me cast exception.

Solution found with Hiberanate str() method and i used hibernate queries.

Please find sample query.

select b.billNumber from Bill b where str(b.billNumber) like :billNumber").setString("billNumber,your_parameter).list();


Thanks
saminda konkaduwa


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.