-->
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.  [ 10 posts ] 
Author Message
 Post subject: Use of net.sf.hibernate.Criteria and net.sf.hibernate.expres
PostPosted: Thu Apr 01, 2004 7:11 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
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:

Code:
<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: Thu Apr 08, 2004 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Have you used aliases as specified in http://www.hibernate.org/hib_docs/reference/html/query-criteria.html#query-criteria-s4 ?

Besides, use Expresion.isNotNull() for your restriction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 4:55 pm 
Senior
Senior

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

I just went ahead and resorted to HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 3:08 am 
Newbie

Joined: Wed Apr 07, 2004 2:49 am
Posts: 5
Yes sir Herr Michael,

CreateAlias worked for me.

Thnx


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 11:07 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
razi wrote:
CreateAlias worked for me.


razi,

If you don't mind I would be curious to know how you accomplished this. Could you possibly post a code sample?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 2:33 am 
Newbie

Joined: Wed Apr 07, 2004 2:49 am
Posts: 5
Here is my Criteria

Criteria criteria = sess.createCriteria(School.class)
.add(Expression.eq("schoolCode", schoolCode))
.add(Expression.eq("schoolStatusICode", "A"))
.createAlias("SchoolCampus", "campus")
.add(Expression.eq("campus.CampusInd", "Y"));

public class School extends PersistentObject
{
private Set SchoolCampus = new HashSet();
}

public class SchoolCampus extends PersistentObject
{
private School chSchool;
private String CampusInd;
}

<class name="persistence.School" >
<set name="SchoolCampus" cascade="save-update" inverse="true" lazy="true">
<key column="school_code"/>
<one-to-many class="persistence.SchoolCampus"/>
</set>

<class name="persistence.SchoolCampus" >
<many-to-one name="chSchool" class="persistence.School" column="school_code"/>
</class>

Hope this is of some help.

In the mean time I got another question for the Hibernate experts. I want to query data which has to be case insensitive. I take L-user input in whatever case and then have to query against what in the table, i.e how to convert(uppercase/lowercase) the data coming in from table and compare against luser input data which I can convert to uppercase.

This wont work.....

.add(Expression.eq("Name", Obj.getName()))

Is there any uppercase function in Criteria API

Razi


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 3:53 am 
Newbie

Joined: Wed Apr 07, 2004 2:49 am
Posts: 5
Razi

RTFApi he he


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 4:31 pm 
Senior
Senior

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

I looked at your example using createAlias but either it doesn't work for my case or I am doing something wrong.

For my POJO structure specified above, I tried the following:



Code:
public List search(UserGroupFunction requestUserGroupFunction)
{
  ...
  ...
  ...
  Criteria criteria = hibernateSession.createCriteria(UserGroupFunction .class);

  if (requestUserGroupFunction.getUserGroup() != null)
  {
    criteria.add(Expression.eq("userGroup", requestUserGroupFunction.getUserGroup()));
  }
  if (requestUserGroupFunction.getFunction() != null)
  {
    criteria.add(Expression.eq("function", requestUserGroupFunction.getFunction()));
  }
  criteria.createAlias("function", "functionData");
  criteria.addOrder(Order.asc("functionData.functionName"));
  return criteria.list();
  ...
  ...
}


The error I am getting is as follows:

Code:
net.sf.hibernate.QueryException: could not resolve property: functionData.functionName of: UserGroupFunction
   at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:50)
   at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:35)
   at net.sf.hibernate.expression.Order.toSqlString(Order.java:37)
   at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:80)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3553)
   at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:223)
   at UserGroupFunctionDAO.search(Unknown Source)
   at admin.UserGroupFunctionAdminAction.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 business.filters.VerifyAdminUserFilter.doFilter(Unknown Source)
   at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
   at business.filters.VerifyLoginFilter.doFilter(Unknown Source)
   at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
   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: Fri May 07, 2004 12:11 am 
Newbie

Joined: Wed Apr 07, 2004 2:49 am
Posts: 5
gpani, have u done a mapping using the set element . Thats how I did my mapping and then have a getter and setter for that set in your object.

Razi


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 8:37 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
razi wrote:
gpani, have u done a mapping using the set element . Thats how I did my mapping and then have a getter and setter for that set in your object.

Razi


razi,

I appreciate your help on this mater. Unfortunately, the answer you gave wasn't for the question I asked originally in this thread. Thanks anyway.


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