-->
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: Multi-Search Criteria using Criteria API
PostPosted: Fri Sep 21, 2007 4:36 pm 
Beginner
Beginner

Joined: Tue May 08, 2007 1:20 pm
Posts: 24
We are having a situation to build a query with dynamically adding "where" clause for a search screen using three different Views .
Wondering if i can use Criteria API to built the query .
For example
Criteria criteria = session.createCriteria(DeviceSummaryView.class)
.createCriteria(DeviceContactView.class)
.createCriteria(HostNameContactView.class);

I am not sure if this works or is supported by Hibernate API as the moment i put this code snippet into Eclipse IDE it gives error saying createCriteria() Not Applicable for arguments

Please note i am able to successfully build & run the query with single pojo i.e. session.createCriteria(DeviceSummaryView.class).

Now i wanna extend it to join with other views such as
session.createCriteria(DeviceSummaryView.class)
.createCriteria(DeviceContactView.class)
.createCriteria(HostNameContactView.class);


Any pointers/suggestions will be highly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 7:36 pm 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
Hi bansilal,

I think you should just write your request like this:
Code:
Criteria criteria = session.createCriteria(DeviceSummaryView.class)
.createCriteria("contactView")
.createCriteria("hostName");



Where you only use the Class definition on the first Criteria (for return type) and then you directly use attribute name to go forward into your data structure (to be able to set constraint on specific attributes). Then you can set constraint on HostNameContactView attribute (address attribute in this example) as follow:
Code:
Criteria criteria = session.createCriteria(DeviceSummaryView.class)
.createCriteria("contactView")
.createCriteria("hostName")
.add(Expression.like("address", "%%")
.list();



Hope its help,

Regards.

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 12:56 am 
Beginner
Beginner

Joined: Tue May 08, 2007 1:20 pm
Posts: 24
Hi Joel
Thanks for quick response. The search screen in my case has fields related to more than one database views.
For example
The Device Search screen in my case has following fields
- hostName & ipAddress related to DeviceSummaryView pojo
- deviceContact related to DeviceContactView pojo
- hostContact & ipAddress related to HostNameContactView pojo

As user may search on any of these fields , i wanna use Criteria query as something like this
Code:
Criteria criteria = session.createCriteria(DeviceSummaryView.class)
.createCriteria(DeviceContactView.class)
.createCriteria(HostNameContactView.class);


so that it result into following query

Code:
select hostName,ipAddress,
deviceContact,
hostContact
[b]from
DeviceSummaryView deviceSummaryView,
DeviceContactView deviceContactView,
HostNameContactView hostNameView[/b]
where
hostName=?
[b]AND
ipAddress=?
AND
deviceContactView.deviceId = deviceSummaryView.deviceId
AND
hostNameView.deviceId = deviceSummaryView.deviceId[/b]


Currently i am able to successfully search on the fields related to single pojo i.e. ipAddress & hostName using session.createCriteria(DeviceSummaryView.class)

Any pointers/suggestions on how to extend this to search on additional fields i.e. deviceContact & hostNameContact related to other pojos will be highly appreciated

Regards
Bansi


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 7:25 am 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
Hi Bansi,

So it seems my answer didn't really solve your problem ? Is it because there is no link between your POJO (DeviceContactView, DeviceSummaryView and HostNameContactView) ?

But in your query, it seems you link them using the deviceId (1..1 relationship).
Code:
select hostName,ipAddress,
deviceContact,
hostContact
from
DeviceSummaryView deviceSummaryView,
DeviceContactView deviceContactView,
HostNameContactView hostNameView
where
hostName=?
AND
ipAddress=?
AND
deviceContactView.deviceId = deviceSummaryView.deviceId
AND
hostNameView.deviceId = deviceSummaryView.deviceId


So at least, their ID are common. So you could define attributes in those Objects which points on other Objects (one-to-one mapping). Then, for example, you will be able to use the following on a DeviceSummaryView instance:
Code:
deviceSummary.getContactView()

This will allow you to use the contactView attribute of the DeviceSummaryView to build your Criteria filter...

I hope its help a bit !

Regards,

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 23, 2007 1:13 pm 
Beginner
Beginner

Joined: Tue May 08, 2007 1:20 pm
Posts: 24
Hi Joël
Thanks for quick response. I highly appreciate your help.
You are absolutely correct
"there is no link between the POJOs (DeviceContactView, DeviceSummaryView and HostNameContactView) "

As there is no link & the POJOs are not associated objects , hence DeviceSummaryView doesnt have attribute "contactView". So its not possible to use
DeviceSummaryView instance to get the ContactView i.e. deviceSummary.getContactView()



I apologize for the query in my earlier posting.Each of these pojos have deviceId as attribute but not sure whether i can link them thru deviceId attribute.

Basically the idea is to accomplish the following

select
<attributes of DeviceSummaryView i.e. ipAddress,hostName>,
<attributes of DeviceContactView i.e. deviceContact>,
<attributes of HostContactView i.e. hostContact>

from
DeviceSummaryView,
DeviceContactView,
HostContactView

where

ipAddress=?
AND
hostName=?
AND
deviceContact=?
AND
hostContact=?

Whenever users search on attributes like devicecontact,
it should look into its corresponding pojo & retrieve the rows

Any pointers/suggestions on how to write criteria query will be highly appreciated

Regards
bansi


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 23, 2007 4:16 pm 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
Hello Bansi,

Quote:
Each of these pojos have deviceId as attribute but not sure whether i can link them thru deviceId attribute.


But in your previous post, in the query, you were joining tables using those ID. It should mean they are the same, no ?

If those tables aren't linked together using Hibernate Mapping, I don't really know how to extract the information you want using a single Criteria query... I would maybe do it using 3 Criteria queries (I'm not an Hibernate expert, so there is maybe another way to do it !).

Regards,

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 27, 2007 4:22 pm 
Beginner
Beginner

Joined: Tue May 08, 2007 1:20 pm
Posts: 24
Hi Joël
Thanks for your thoughts. I tweaked the pojos so that they are linked using the ID i.e deviceId in my case.

Now i have two pojos using deviceId
Code:
public class DeviceSummaryView  implements java.io.Serializable {
         private long deviceId;

//As suggested by Joël
private DeviceContactView deviceContactView;

    // Constructors

    public DeviceContactView getDeviceContactView() {
      return deviceContactView;
   }

   public void setDeviceContactView(DeviceContactView deviceContactView) {
      this.deviceContactView = deviceContactView;
   }
}


Code:
public class DeviceContactView  implements java.io.Serializable {
      private long deviceId;
     private String lastname;
     private String firstname;
}



As suggested by you i have added the attribute into DeviceSummaryView but not sure how to add this change into Hibernate Mapping File. I tried the following snippet but it resulted in Mapping Exception
Code:
<hibernate-mapping>
    <class name="com.boeing.nmt.nams.model.DeviceSummaryView" table="DEVICE_SUMMARY_VIEW">
[b[color=red]]<many-to-one name="deviceContactView" class="com.boeing.nmt.nams.model.DeviceContactView" fetch="select">
            <column name="DEVICE_ID" precision="10" scale="0" not-null="true" />
        </many-to-one>[/[/color]b] </class>
</hibernate-mapping>



Logically its many-to-many relationship between device & contacts. But in the case of Search screen all i do is key in the contact name & the search resultSet will display the list of devices i.e. i guess one-to-many uni-directional relationship as you key in the contact name on search screen & it will show the list of devices associated with contact. Not sure how to model this in hibernate mapping file

Any pointers/suggestions highly appreciated


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.