-->
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.  [ 5 posts ] 
Author Message
 Post subject: Usage of like condition in Hibernate for Criteria Builder
PostPosted: Fri May 10, 2013 4:04 pm 
Newbie

Joined: Fri May 10, 2013 3:57 pm
Posts: 4
I am using Hibernate 4.0 and I have the following in DAO class for my filter condition

Code:
ParameterExpression<Long> pexp = criteriaBuilder.parameter(Long.class,"empNo");
criteria.add(criteriaBuilder.like(emp.<String>get(String.valueOf(Employee_.empNo)),
String.valueOf(pexp)));


However when I run I am getting the following exception

Code:
Unable to resolve attribute [org.hibernate.ejb.metamodel.SingularAttributeImpl$Identifier@a21c8
a] against path
java.lang.IllegalArgumentException: Unable to resolve attribute [org.hibernate.ejb.metamodel.Singula
rAttributeImpl$Identifier@a21c8a] against path
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:116)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:221)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:192)


How can I fix this issue?

I have tried as

Code:
Predicate predicate = criteriaBuilder.like(emp.get(Employee_.empNo).as(String.class),
                  pexp.as(String.class));


but the generated sql shows as

Code:
where cast(emp0_.EMP_NO as varchar2(255 char)) like cast(? as varchar2(255 char)


Top
 Profile  
 
 Post subject: Re: Usage of like condition in Hibernate for Criteria Builder
PostPosted: Fri May 17, 2013 6:13 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi .

why dont you have duplicate column with formula field and you could cast as to_char(column).

The same can be used in filter condition with out type casting.

Please let know how this worked.

Cheers!

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Usage of like condition in Hibernate for Criteria Builder
PostPosted: Fri May 17, 2013 4:12 pm 
Newbie

Joined: Fri May 10, 2013 3:57 pm
Posts: 4
shynate

Thanks for the reply.

You mean another field for
Code:
empNo
as String in Entity class?

Could you be kind enough to point to some example or any code snippet?

Appreciated.


Top
 Profile  
 
 Post subject: Re: Usage of like condition in Hibernate for Criteria Builder
PostPosted: Fri May 24, 2013 5:32 am 
Newbie

Joined: Fri May 10, 2013 3:57 pm
Posts: 4
I have added a duplicate column in Entity as
Code:
private String empNoDup;
@Column(name = "employeeNumber, insertable = false, updatable = false")


with getter and setter

I am getting the following exceptions

Code:
org.hibernate.exception.SQLGrammarException: ORA-00923: FROM keyword not found where expected

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ORA-00923: FROM
keyword not found where expected


I believe empNoDup is treating as a database field and it is expecting it to be part of the query? Or should I declare it as transient?
If it is transient then would I be able to use for filter?

Thanks


Top
 Profile  
 
 Post subject: Re: Usage of like condition in Hibernate for Criteria Builder
PostPosted: Fri May 24, 2013 1:59 pm 
Newbie

Joined: Fri May 10, 2013 3:57 pm
Posts: 4
Thank you shynate26 for providing the idea of duplicating the column for filtering, it works very well. Appreciated.

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.