Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 3.0 Questions

 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just get started with Hibernate for the second day. I found it very powerful in data persistence, but it is kind of complicated, and requirs a lot of coding, even for small things like running a query and get the result in a Java collection, such as a ArrayList or a Vector. For example, in the following code snippet, instead of mapping the query result to class AuctionInfo, is there anyway to map it to a java collection class? If the intention is only retrieving the data from database for display only, there is no need to create a object to map it. Would Hibernate be an overkill in this case?

Thanks in advance for your reply.


List auctions = s.createQuery(
"select new AuctionInfo( item.id, item.description, item.ends, max(bid.amount) ) "
+ "from AuctionItem item "
+ "left join item.bids bid "
+ "group by item.id, item.description, item.ends "
+ "order by item.ends desc"
)
.setMaxResults(100)
.list();

Iterator iter = auctions.iterator();
while ( iter.hasNext() ) {
AuctionInfo ai = (AuctionInfo) iter.next();
System.out.println(
"Auction: " + ai.getId() + " - " + ai.getDescription() +
", ends: " + ai.getEnds() +
", highest bid: " + ai.getMaxAmount()
);
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are two other ways to query with Hibernate, there is HQL. But that looks almost like SQL.

Then there is the Criteria Object. You can use to do all the stuff you have in your query. It is less code in my opinion. You won't need a group by, but it can do aggregates.

And while you need an object to store the data, because usally you want to pass that somewhere, or if it for future calculations, just get the information from the SQL, you won't need an object there.

I think that made sense, right?

Mark
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Thanks for the reply. What I am looking for is some kind of JDBC wrapper feature in Hibernate that allows to run a simple query and get the the result set mapped in a Java collection, without heavy coding of the mapped java object, not mentioning the hibernate xml property file.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coming right up. You can use straight SQL as a query, I will open up my IDE and post a sample.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a generic execute query method. The string you would replace or send to the method is the SQL Query.

It returns a ResultSet



Mark
[ November 03, 2005: Message edited by: Mark Spritzler ]
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Is this HibernateUtil class written by you? I do not see it in the hibernate3.jar. Besides, this is pretty much the same as the regular JDBC call.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is written in-house. Sorry I didn't state that.

Yes basically you are using the Session and stuff of Hibernate to do the JDBC stuff on your behalf.
 
Magesh Lakshmi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,

If you use Hibernate + Spring [ most preferred combo ], then Spring has a JDBCTemplate, where in jdbc calls are much simplified. Even you can create an object directly out of the resultset, without you populating the object.
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank - you may just find it easier to copy the HibernateUtil.java class from the Caveat Emptor application at hibernate.org.

Download the source, pull that file out, and use that as your Hibernate utility class for session management.

You'll see HibernateUtil all over the place while reading documentation so just train yourself w/ the pre-written one and don't concern yourself too much w/ the implementation details. Session management can be a pain so it is best to pick a strategy and stick w/ it.

If you're serious about using Hibernate, make sure you pick up a copy of Hibernate In Action by Christian Bauer and Gavin King. The example you posted at the top can be explained in detail in chapter 7 of the book...for writing report queries.

I'm working on doing the same thing right now, coincidentally...and the example you posted is accurate (in theory)...you can create an object on the fly w/o having to manually create & map it...get the book for details. I wish I could help more but I'm just beginning to experiment...I'll report my progress this weekend.

-v
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,
Thanks a lot for the replies. Your guys are the best! After three days' endeavor on Hibernate, I decided to put it to shelf for now. We are pretty much a Weblogic shop, and in a need for a productivity tool in database access/persistence layer. We did not choose Entity Beans because the concensus towards it is its complexity. Maybe I am more familiar with Entity Beans, I felt Hibernate is as complex as Entity Beans, if it is not more complex. It might makes sense if you do not have a full-blown application server, or I don't see why you will pick Hibernate over Entity Beans which at least to have a spec to go by and supported by the whole industry. Besides, Hibernate has a very big foot print and might have conflicts with the application server if you use it in your session bean for persistence. One thing I found out is that the POJO mapping class will not be reloaded when you redeploy your session bean which contains the POJO mapping class, until restarting the server. Also I believe you have more flexibility with Entity Beans because you can at least have your BMP as a choice. There might be good use case for Hibernate, but in real world, you need to have a good mix to leverage both Java/J2EE, and the legacy database objects, such as Stored Proc. I will take a look at the Spring JDBC template and thanks a lot for your information.
Above is just my 2 cents to share. 3 days' work definitely will not make me an expert in the area. Do not be offended by my opinions if you like Hibernate and good luck with your Hibernate projects.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're not offended. But, the exact cases why you chose not to use Hibernate is excatly why you need to use hibernate. Except the complexity part. Hibernate is much much more easier, and I have seen the complete opposite when it comes to footprint, speed, ease of integration with App Servers. Hibernate has nothing to do really with whether you are running in an App Server or not, the code stays the exact same.

Also, if you intend to go to EJB 3.0, then get used to Hibernate because that is a direct implementation of the EJB 3.0 Entity Bean Specification, so you will learn it one way or another.

And I love tools, frameworks, and products that save me at least 1/2 my normal development time. With Hibernate, I am down to 1/10 the time it would take me without it. In 30 minutes I can get an Object Model mapped to a relational, yes, some many to ones and other ways around, database table created and mapping generated. I don't have to type any mapping, it gets automatically generated through Ant, using Annotations in the POJO is quick and easy.

Anyway, have fun. Do it how you want to do it. I won't stop you. People have different opinions. And I leave it at that.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic