-->
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.  [ 6 posts ] 
Author Message
 Post subject: native sql
PostPosted: Wed Oct 04, 2006 1:19 pm 
Newbie

Joined: Wed Apr 19, 2006 11:17 am
Posts: 5
This is probably really simple but going through different documentation its hard to find a clear example...


I want to use native sql with no entity mappings to select data so here is the senario:

Table

create table USER(
USER_ID INT(15) NOT NULL,
FIRSTNAME VARCHAR(15) NOT NULL,
SURNAMR VARCHAR(15) NOT NULL,,
PRIMARY KEY (USER_ID));

I know how to do this using mapped entities but I really want to run a native sql query like:

select * from user where user_id = ?;

and get a resultset of some sort back and access each column one by one (just like raw jdbc). If possible it would be nice to see how the update on a row is done as well.

I'm using hibernate 3. Any help would be appreciated.


Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 1:56 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Try something like this.


String queryString= "select {u.*} from user where u.user_id=:id";
SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(queryString.toString());

query.addEntity("u",User.class);
query.addScalar("id",id);

List results = query.list();

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 5:33 am 
Newbie

Joined: Wed Apr 19, 2006 11:17 am
Posts: 5
thanks for that... but I was wondering whether there was a way without associating an entity to it through using a session.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 1:13 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
You need to specify somewhere how to map from the result set to an object. With hibernate 3.1 you need to create a mapping but you do not need to associate it with a table. I took this approach a couple of times. At other times I just bypassed Hibernate and used Spring's based RowMapper. It was pretty painless.

There are a bunch of features in 3.2. It seems lots of people were asking about this. If you can work in 3.2 you should give it a try.

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:28 pm 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
Hi Marius

I was very happy looking at your posting as it answers my question but i figure out there is syntax error in

query.addScalar("id",id);


Pl note the second argument to addScalar has to be Type i.e. Hibernate.String or Long

Anyhow i got the clue and thanks for your help.

Now i enhance my sql query to have many-to-many relationship but it doesnt work . Any pointers/suggestions will be highly appreciated. Here is my query
http://forum.hibernate.org/viewtopic.ph ... tesqlquery


Regards
Bansi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 2:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the addscalar he refers to works fine if you are using Hibernate 3.2 which as he says have seen great usabilty improvements in this context.

_________________
Max
Don't forget to rate


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