-->
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.  [ 9 posts ] 
Author Message
 Post subject: user HQL to query a table
PostPosted: Fri Jul 07, 2006 9:49 am 
Newbie

Joined: Fri Jul 07, 2006 6:51 am
Posts: 4
Dear all,

I am a beginner of hibernate. I have the following question.

From what I understand, HQL can be used to query an object. Can I use HQL to directly query a table from database??

Thanks in advance for any help.

Jake


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 9:54 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
yeah you can run a HQL like this

from Order

which, if Order is mapped to a table called Order would be the same as

SELECT * FROM ORDER

You can read the documentation with examples here:
http://www.hibernate.org/hib_docs/v3/re ... ryhql.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 10:01 am 
Newbie

Joined: Fri Jul 07, 2006 6:51 am
Posts: 4
Thanks for the prompt reply. Maybe I didn't make this clear..

If I have a table in mysql database and this table is not mapped from any object..

Can i query such a table while it's not mapped from any object?

Thanks,

jake


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 10:31 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Quote:
If I have a table in mysql database and this table is not mapped from any object..

Can i query such a table while it's not mapped from any object?


using hql, no you cannot

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 10:33 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
double post

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 11:03 am 
Newbie

Joined: Fri Jul 07, 2006 6:51 am
Posts: 4
Guys, thanks a lot for answers.

Now I am having this realistic scenario and i am stuck with how i can use HQL to query what I want..

I have 2 classes, Person.java and Role. java (A person can possess multiple roles, e.g. a person can be a student, a researcher, an employee at the same time...)

Role.java class is a simple class to define a role, such as a student, a researcher, an employee, etc. No more than that.

Inside Person.java class, I am using a Set to contain all the roles this particular person can possess...

By using hibernate, I now have a table (in my Mysql database) called Person which is mapped from Person.java class, another table called Role which is mapped from Role.java class.

To demonstrate the mapping between a person and the roles he can have, I now also have a table called person-role-mapping, which has two columns (person & role) to list the pairs. The data in this table is obviously generated from the Person.java class, going through the set of roles that a person can have. For example, a person called Jake can have a set of roles, be they a student, a reseacher, an employee.. So in the person-role-mapping table, jake-student is in a row, jake-researcher is in a row, jake-employee is in row..

Of course there will be more in the table, since a role, e.g. a student, can be possessed by many persons.

NOW, here is the problem I am stuck with...

I want to use a HQL query to ask the person-role-mapping table to give a list of persons that have a same role, e.g. please return all the persons who are students to me..

Since this person-role-mapping table is not directly mapped from any class as described above, I was really struggling with how I can use HQL to make this query..

I do really appreciate if someone could help me out with this problem.

Many thanks, Jake


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 11:36 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
If you dont have mapping for a table( ex: person_role_mapping ) which doesnt have hibernate mapping file, then you cant use it in your HQL's. An approach would be to use direct SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 11:50 am 
Newbie

Joined: Fri Jul 07, 2006 6:51 am
Posts: 4
Yes, I was also thinking of this alternative, i.e. use SQL...

I have the following code to create a SQL query. It however returned errors for me.

session.createSQLQuery ("SELECT userid FROM infomodel.personrolemapping WHERE roleid='" + roleId + "'").addEntity(..., ...)

I dont think I have any problem with this SQL command. However it seems you must use addEntity() or addScaler() methods when you call a SQL query. Is this the case?

I cannot figure out what I should really specify as parameters in the addEntity() or addScaler() methods.

Any tips on how to correctly use addEntity() or addScaler() with the SQL query??

Thanks. Jake


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 7:42 pm 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
hmm....

you might wanna work directly with Connection objects, in which case you could use whatever sql you could run normally, and receive a ResultSet.

Is that what you want?

A bit too late here to find the actual code you would use, but it shouldn't be too dificult to find ;)

Please rate any comments as helpful if they are ;)

Ok, just about awake enough now to give you the code, if you have a session in a transaction you can do something like this:

ResultSet rs = session.connection().createStatement().executeQuery("SELECT * FROM order");


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