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: Having Problem Generating Correct Query Using Hibernate
PostPosted: Thu Aug 14, 2008 1:29 pm 
Newbie

Joined: Thu Aug 14, 2008 1:13 pm
Posts: 4
I have a table with name value pairs in a generic data base.

I am using Criteria functionality to try to get the results out. However, I am running into problem with using multiple criteria.

For Example if the table contains name value pairs of First Names and Last Names. I can use criteria to get all the matches for First Name, and I can use to get matches for Last Names. But if I try to get results, which have First Names associated with Last Names, I can't get it working.

I am not sure what I am doing wrong

Here is an example of the code I am using


Code:
Criteria c = session.createCriteria(Some.class)
c.add(Expression.eq("column","FirstColumns"));
c.add(Expression.eq("data","FirstColumnData"));
c.add(Expression.eq("column","SecondColumns"));
c.add(Expression.eq("data","SecondColumnData"));


if I just use either First Column or Second Column for looking up values, everything is fine. But when I try both, nothing comes back. Any ideas? Am I clear with my question.

Thanks for all the help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 2:15 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not sure what you want to achieve ? Do you need an or condition for firstname and lastname ?

Code:
Criteria c = session.createCriteria(Some.class)
c.add(
  Expression.Or(
          Expression.eq("FirstName", firstName),
          Expression.eq("LastName", lastName),
));


And I'm not sure what you mean with columns in your example. Keep in mind that HQL works on properties not columns.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 2:53 pm 
Newbie

Joined: Thu Aug 14, 2008 1:13 pm
Posts: 4
Sorry I don't think I was clear. From what I understand the table is structured generically, so Essentially there are two columns in the table. One for Columns Names, and the other is for values associated with it. So it would look something like this:


Code:
Column1:Column2
FirstName:Fname1
FirstName:Fname2
FirstName:Fname3
LastName:Lname1
LastName:Lname2
.
.
.


What this allows us to do is add a new column, w/o changing the table. When I try to use both criterias, as I explained before, to me it seems like Hibernate is trying to match a record which has both First Name and Last Name, yet all records can have only First Name or Last Name and not both.

But I need to get the record, which contains a First Name in the Fname column and Last Name in the Lname column, so they belong to the same person.

And I have tried using "and" operators, and I get nothing back. When I use "or" operators, it brings back all matches for First Name, and then all matches for Last Name, but it doesn't look at the results to see if they are associated with each other. Essentially doing two different calls. Its like it doesn't know how First Names and Last Names go together.

By columns I do mean properties. I am sorry about that, I am still learning Hibernate lingo.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 2:55 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your mapping ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 3:03 pm 
Newbie

Joined: Thu Aug 14, 2008 1:13 pm
Posts: 4
wolli wrote:
Can you post your mapping ?

I am not sure I know that that is, you mean the table structure?

and I thought about this some more, I think I need something like an inner join.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 3:55 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The part where you "map" classes and properties to tables and columns. Looking at your code sample I suppose you really meant "hibernate" and are programming in java ? You got the wrong group, this is NHibernate, the port to .Net.

But when you post the "mapping" information I can help, but posting to the Hibernate part of the forum won't hurt.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 12:20 pm 
Newbie

Joined: Thu Aug 14, 2008 1:13 pm
Posts: 4
Thank you for your answers. I talked to some folks at work and we decided to create a dynamic query. That did the job. It seems at this point its somewhat difficult to create a crieteria search with name-value pairs in Hibernate.


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.