-->
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.  [ 7 posts ] 
Author Message
 Post subject: Use of net.sf.hibernate.Criteria for querying
PostPosted: Fri Apr 02, 2004 11:06 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
I posted this question under the miscellaneous forum and maybe it belongs in the Beginners forum instead.

I am using Hibernate 2.1.2 with Resin 2.1.12 on SAPDB 7.4.

I have been trying to stay away from HQL as much as possible and accomplish as much as I can using Criteria. I have an object that has the following structure.


Code:
public class UserGroupFunction
{
  ...
  private UserGroup userGroup = null;
  private Function function = null;
  ...
}

public class UserGroup
{
  private Long userGroupId = null;
  ...
}

private class Function
{
  private Long functionId = null;
  private String functionName = "";
  private String functionUrl = "";
}




The mappings in UserGroupFunction are as follows:

    <many-to-one name="userGroup" class="com.foo.bar.beans.site.access.UserGroup" cascade="none" outer-join="false" update="false" insert="true" column="USERGROUPID" not-null="true" unique="false" />

    <many-to-one name="function" class="com.foo.bar.beans.site.access.Function" cascade="none" outer-join="true" update="false" insert="true" column="FUNCTIONID" not-null="true" unique="false" />



In the search() method in my DAO, I would like to accomplish two conditionals.

1. WHERE function.functionUrl IS NOT NULL
2. ORDER BY function.functionName

I tried to do it with the following code (out of desparation) but obviously it was too stupid and didn't work.

Code:
criteria.add(Expression.not(Expression.eq("function.functionUrl", null)));
criteria.addOrder(Order.asc("function.functionName"));



Is there any way to set conditions based on attributes within an object contained in the persistent object using the Criteria method or do I have to resign myself to doing this with HQL?

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2004 5:57 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Is there any additional information that I can provide to clarify my question? If it is very rudimentary, could someone point me towards a source where I can research it further? I can't find anything on Google or in sample applications.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2004 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Code:
critOne = s.createCriteria(UserGroupFunction.class);

critTwo = critOne.createCriteria("function").add(Expression.not(Expression.eq("functionUrl", null)));

critTwo.addOrder(Order.asc("functionName"));

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:29 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Christian,

Thanks for the response. I tried to create a second criteria using the original criteria to order by functionName of the parentFunction. Any suggestions?

Code:
hibernateSession = HibernateManager.currentSession();
Criteria criteria = hibernateSession.createCriteria(FunctionRelation.class);

if (requestFunctionRelation.getParentFunction() != null)
{
  criteria.add(Expression.eq("parentFunction", requestFunctionRelation.getParentFunction()));
}
if (requestFunctionRelation.getChildFunction() != null)
{
  criteria.add(Expression.eq("childFunction", requestFunctionRelation.getChildFunction()));
}

Criteria functionCriteria = criteria.createCriteria("parentFunction");
functionCriteria.addOrder(Order.asc("functionName"));

functionRelationList = functionCriteria.list();




The following is the Exception stack trace

Code:
Stack Trace: java.lang.UnsupportedOperationException: subcriteria cannot be ordered
   at net.sf.hibernate.impl.CriteriaImpl$Subcriteria.addOrder(CriteriaImpl.java:75)
   at com.foo.bar.dao.site.access.FunctionRelationDAO.search(Unknown Source)
   at com.foo.bar.control.site.access.admin.FunctionRelationAdminAction.view(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
   at org.apache.struts.actions.LookupDispatchAction.execute(LookupDispatchAction.java:252)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:126)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
   at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
   at com.caucho.server.http.Invocation.service(Invocation.java:315)
   at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
   at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
   at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
   at java.lang.Thread.run(Thread.java:534)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:33 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use HQL.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:35 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
christian wrote:
Use HQL.


I think that would be the easiest option. I'll go ahead and do that. I was just wondering if it would've been possible with Criteria.

On a different note, are there any plans to improve on the Criteria API to make it as mature as HQL in future releases?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 6:56 pm 
Newbie

Joined: Sun Feb 08, 2004 9:30 am
Posts: 8
Location: Europe
gpani wrote:
christian wrote:
Use HQL.


I think that would be the easiest option. I'll go ahead and do that. I was just wondering if it would've been possible with Criteria.

On a different note, are there any plans to improve on the Criteria API to make it as mature as HQL in future releases?


Ordering by nested objects using the crit-api is still under development. Gavin once psoted that it's on the list (I'm waiting 4 it, too *g*), but not top-priority.


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