views:

124

answers:

2

We've been implementing Hibernate recently as a replacement for JDBC.

What I like is not having to constantly write SELECT, UPDATE, INSERT statements and the associated PreparedStatement and ResultSet code.

However we've been struggling with random bizarre behavior (example A) which I find hard to understand and resolve due to all the different configuration/feature options and the associated Hibernate behavior. I find some of the features like caching, lazy loading, etc, etc very cool but way more than I need - and ultimately confusing.

Is there a better middle ground for someone just looking to avoid the tediousness of JDBC but who doesn't need all the features of Hibernate?

+5  A: 

not completely avoiding jdbc, but ... my suggestion is to use the jbdc support provided by spring framework. You still need to write your select, update and inserts, but spring nicely wraps it so you usually don't care about the result set, closing connections, cleaning up your code and such.

Have a look at this chapter from the spring documentations to see the details. You can create an entire dao layer that appears just like the hibernate dao layer, but the internal implementation is different. The RowMapper interface lets you handle the conversion from result sets to objects very nicely. Overall it provides a clean separation of concerns.

(another alternative is to use iBATIS for lightweight O/R mapping, or at least to keep your sql queries outside of Java code).

Yoni
+1 for iBATIS. Yes, iBATIS is what you are looking for. Use the iBATIS 3 mapper to get off the jdbc stuff in your code.
Arne Burmeister
iBATIS still does not alleviate the issue of writing SQL by hand.
Kevin
It's also poorly documented
skaffman
iBATIS is horrid. the issue with ORM Frameworks is that they're easy to sell as being easier to use (e.g. developers dont need to know sql) and then u end up having to deal with, as the original poster put it -- bizarre behavior -- related to things like caching, sessions etc. Maintenance is incredibly easier if you simply go with a basic DAO pattern and use a JDBC wrapper using something like the JDBCTemplate in Spring (as others have said).
BestPractices
You can also wind up having to tweak ORM mapping configuration and/or generated SQL anyway for things like fixing performance issues (e.g. when the ORM Framework generates 1000 queries rather than the 1 or 2 it could have generated). Then the next developer you bring on doesnt know that the previous developer fixed the ORM configuration's so that it brings back data using 1-2 queries rather than 1000 and changes the configuraiton back to its original poor performing state to fix some issue they're having in another piece of code that's using the same ORM query. Just write SQL and be done w/ it
BestPractices
I tend to agree with @Yoni and @BestPractices. I was hoping for an alternate approach (the "middle ground") but it seems this doesn't exist.
Marcus
A: 

How about using Hibernate (or TopLink) as a JPA provider. IMO I find the JPA annotation-based approach to doing ORM a lot easier to understand / implement than Hibernate directly - and you can always drop down and do 'difficult' stuff with Hibernate directly.

Nate