-->
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.  [ 3 posts ] 
Author Message
 Post subject: New to hibernate - First Hibernate
PostPosted: Mon Mar 08, 2004 7:07 pm 
Beginner
Beginner

Joined: Mon Mar 08, 2004 5:22 pm
Posts: 35
Here is my sql query. what are my options ?
1. Do i need to create separate java class for each table ?
2. Why can't i create a class with all the selected fields [abstact] and run the regular SQL in session.find()?

Pardon me. This is my first hibernate attempt.

--------------------------------------------------------------------------
select l.zip_code, l.city, l.state, f.sky24, c.temperature,c.wind_speed, c.relative_humidity
from weather_location l, weather_forecast f, weather_current c
WHERE l.zip_code = 30346
and l.station_id = c.station_id
and l.location_id = f.location_id
and f.day_of_week = 2
order by l.zip_code;
---------------------------------------------------------------------------

Thanks
Raj


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hibernate is an ORM thus is is designed to map Object Class to Relational tables. You need to create the class mapping for the tables and then either load the objects or use HQL (hibernate query language) to query for the results. Direct SQL can be used but it is only there for the 5-10% of complex situations that require going outside the box. Yours is not one of them. I suggest you try a few of the examples and read the manual as a starting point.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 8:37 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
You would want to define classes for WeatherLocation, WeatherForecast, and WeatherCurrent. Then the question is, what exactly are you trying to achieve in this SQL? You can certainly achieve the same thing in Hibernate -- the HQL transliteration of this SQL would look something like:

Code:
select l.zip_code, l.city, l.state, f.sky24, c.temperature, c.wind_speed, c.relative_humidity
from WeatherLocation l, WeatherForecast f, WeatherCurrent c
where l.zip_code = 30346 and l.station = f and l.location = c
and f.day_of_week = 2 order by l.zip_code;


Of course, Hibernate is buying you very little if you do this... you are just pulling fields (projecting columns) out of your objects. Hibernate only really starts paying dividends when you build your application around it and start thinking more in terms of the objects than in terms of the columns you are pulling out of them. Without knowing more about your application it's hard to know what this query *means* and hence how to make it more Hibernate-ish.

Now, when it comes to creating objects, Hibernate will certainly help right away -- it's much nicer to do 'session.save(new WeatherLocation("San Francisco", "CA", "94105"))' than to do whatever JDBC you are doing now....

Cheers,
Rob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.