-->
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: Help! JPA using Hibernate
PostPosted: Sun Jun 08, 2008 2:29 pm 
Newbie

Joined: Thu Aug 11, 2005 12:15 am
Posts: 9
How can I convert the following Hibernate code to JPA and get the same functinoality?

Criteria criteria = session.createCriteria(Article.class);
String[] splits = searchTerms.split(" ");
criteria.add(Restrictions.eq("blog", false));
criteria.add(Restrictions.eq("deleted", false));

for (int loop = 0; loop < splits.length; loop++) {
String withWildCards = "%" + splits[loop] + "%";
criteria.add(Restrictions.ilike("title", withWildCards));
}
criteria.addOrder(Order.desc("postDateMillis"));
criteria.setFirstResult(Integer.parseInt(index));
criteria.setMaxResults(Constants.MAX_PAGE_SIZE);
List<Article> articles = criteria.list();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 08, 2008 6:04 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Which JPA implementation are you using?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 08, 2008 6:51 pm 
Newbie

Joined: Thu Aug 11, 2005 12:15 am
Posts: 9
I'm using javax.persistence 1.0 with Hibenate 3.2.6.ga as the provider.

But constructing my own query from parts and pieces of strings, I've pretty much got it doing what I want except I don't know how to deal with

// criteria.setFirstResult(Integer.parseInt(index));
// criteria.setMaxResults(Constants.MAX_PAGE_SIZE);


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 08, 2008 9:10 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
criteria.setFirstResult(0);
criteria.setMaxResults(25);

Sorry, I'd just done a copy and paste from my website. The variable names make sense in context of the webpage code, but alone it doesn't. The arguments just take an integer as parameter.

:)

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 11:39 am 
Newbie

Joined: Thu Aug 11, 2005 12:15 am
Posts: 9
Thanks ... I figured out the ...

// criteria.setFirstResult(Integer.parseInt(index));
// criteria.setMaxResults(Constants.MAX_PAGE_SIZE);

piece ... left an message about that but looks like it wasn't saved.

Using JPA, it appears that I have to construct my query by manipulating parts and pieces of strings rather than the clean way that hibernate gives you using the criteria API. Other than the ability to use other service providers, I'm beginning to wonder what the real benefit of JPA is ... I'll admit that might be a naive question as I've only started looking at JPA, but so far, I'm not seeing the advantages.


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.