-->
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.  [ 4 posts ] 
Author Message
 Post subject: like search on Integer type fields using HQL or criteria
PostPosted: Sat Dec 25, 2010 10:01 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hi!, I'm new to hibernate and I've one issue regarding like search in Hibernate. Can any one tell me that How to perform like search on a field that is defined as Integer or BigInteger in POJO? e.g.

class Employee { private Integer id; private String name; }

how to perform like search on ID using HQL or criteria queries from my search screen? If i try to perform in a normal way then it throws ClassCastException.

Please help me out as I'm stuck in this issue.


Top
 Profile  
 
 Post subject: Re: like search on Integer type fields using HQL or criteria
PostPosted: Sat Dec 25, 2010 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
hi, do you have an example of what you're trying to do? a code snippet of your HQL and model?
Not literally using "like" operator I hope?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: like search on Integer type fields using HQL or criteria
PostPosted: Sun Dec 26, 2010 1:26 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Dear Sanne,
below is my code snippet...

StoredIn.java

@Entity
@Table(name = "STORED_IN")
public class StoredIn implements java.io.Serializable {
private static final long serialVersionUID = -4142896760561230746L;
private BigDecimal mediaId;
private Media media;
private Location location;

public StoredIn() {
}

public StoredIn(Media media) {
this.media = media;
}

public StoredIn(Media media, Location location) {
this.media = media;
this.location = location;
}

@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "media"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "MEDIA_ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getMediaId() {
return this.mediaId;
}

public void setMediaId(BigDecimal mediaId) {
this.mediaId = mediaId;
}

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Media getMedia() {
return this.media;
}

public void setMedia(Media media) {
this.media = media;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "NAME")
public Location getLocation() {
return this.location;
}

public void setLocation(Location location) {
this.location = location;
}

}

StoredInDAOImpl.java

@Override
public Object searchStoredIn(Object... objects) {
DetachedCriteria dt = DetachedCriteria.forClass(StoredIn.class, "storedin");
dt.add(Property.forName("storedin.mediaId").like(objects[0].toString(),MatchMode.ANYWHERE)); /* this line throws Class cast exception since mediaId is BigDecimal field in StoredIn.java.*/
dt.add(Property.forName("storedin.location.name").like(objects[1].toString(),MatchMode.ANYWHERE));

return getLibmsHibernateTemplate().findByCriteria(dt);
}

while above method executes the values in Object array are object[0] = 12 and object[1] = UK.

My question is how would i perform like search on mediaId since my actual media id is 123456 now i want to search this id by keyword 12 using like search. in SQL if i run query select * from Media where mediaId like %12%. it fetch the proper result. I want to do the same using hibernate also.


Top
 Profile  
 
 Post subject: Re: like search on Integer type fields using HQL or criteria
PostPosted: Mon Dec 27, 2010 8:29 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
like operator requires a string type, so you can use the str function to get a string representation:
Code:
   Query query = session.createQuery( "select si from StoredIn as si where str(si.mediaId) like :likeWhat" );
   List list = query.setParameter( "likeWhat", "1%" ).list();

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.