Java Reference
In-Depth Information
The advantages of groovy.sql.Sql over raw JDBC are obvious. If I have SQL code
already written, I always use it.
Lessons learned (Groovy SQL [ 5 ] )
1 . The groovy.sql.Sql class makes working with raw SQL better in every way:
resource management, multiline strings, closure support, and mapping of result sets to
maps.
2 . Related examples in this topic can be found in chapter 7 on Spring and chapter 9
on REST.
5 Worst SQL Joke Ever Told: SQL query walks into a bar, selects two tables and says, “Mind if I join you?” (rim-
shot). (Warning: NoSQL version later in this chapter.)
Rather than write all that SQL, you can instead use one of the object-relational mapping
(ORM) tools available, the most prevalent of which is still Hibernate. The Java Persistence
API (JPA) specification acts as a front-end on ORM tools and is the subject of the next sec-
tion.
8.3. The Java approach, part 2: Hibernate and JPA
Oneapproachtosimplifying JDBCistoautomate asmuchofitaspossible. Theearlyyears
of Java saw attempts to add ORM tools directly to the specification, with varying degrees
of success. First came Java Data Objects, which worked directly with compiled bytecodes
and are largely forgotten today. Then came Enterprise JavaBeans (EJB) entity beans, which
were viewed by the community as a mess in the first couple of versions.
As frequently happens when there's a need and only an unpopular specification available,
the open source community developed a practical alternative. In this case the project that
emerged was called Hibernate, which still aims to be the ORM tool of choice in the Java
world when dealing with relational databases.
In regular JDBC a ResultSet is connected to the data source as long as the connection
is open, and goes away when the connection is closed. In the EJB world, therefore, you
 
 
Search WWH ::




Custom Search