developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Website Load Testing
Imprinted Gifts
Compare Prices
Shop Online
PDA Phones & Cases
Imprinted Promotions
Best Price
Free Business Cards
Find Software
Home Improvement
Memory Upgrades
Memory
Web Hosting Directory
Logo Design

 


Turbo Screen Sharing
Adobe Acrobat Connect Professional offers users the ability to have a more productive and engaging web conferencing experience while providing the IT department with a program that efficiently utilizes bandwidth and minimally impacts the infrastructure. Learn More! »

Informal Learning: Extending the Impact of Enterprise Ideas and Information
Forward-thinking organizations are turning to enterprise learning in their quest to be better informed, better skilled, better supported at the point of need, and more competitive in their respective marketplaces. Learn More! »

Rapid E-Learning: Maturing Technology Brings Balance and Possibilities
Rapid e-learning addresses both time and cost issues by using technology tools to shift the dynamics of e-learning development. Learn why more skilled learning professionals use these tools and how you can get a solution to keep pace with your business demands. »

Delivering on the Promise of ELearning
This white paper defines the framework to launch e-learning as a set of teaching, training, and learning practices not bound by a specific technology platform or learning management system. It offers practical suggestions for creating digital learning experiences that engage learners by building interest and motivation and providing opportunities for active participation. »
Developer News -
iPhone Knocking on the Enterprise    July 14, 2008
Linux 2.6.26 Opens Up to Debugging    July 14, 2008
Borland Launches ALM Management Tools    July 14, 2008
Nominations Open for CEO Vision Awards    July 11, 2008
Free Tech Newsletter -

Download: Visual Report Development Tool. Meet virtually any reporting requirement and eliminate the need to code your reports by hand.

Using iBatis SQL Maps for Java Data Access
By Michael Klaene

Go to page: Prev  1  2  3  

The final two examples involve a scenario almost every Java applications will encounter. Objects do not exist in their own isolated bubble. Rather, objects have relationships with other objects and these relationships determine behavior. The room_reservations table is linked to conference_room by the room_id column. In the world of Java, however, this might manifest itself in a java.util.List attribute on the ConferenceRoom bean. This List would contain all associated RoomReservation beans. We will modify the ConferenceRoom JavaBean by adding the reservationsList property. Now, if we wish to populate a ConferenceRoom bean, we will want its List of RoomReservations too.

(SQL MAP)

  <result-map name="get-conference-room"
              class="entities.ConferenceRoom">
     <property name="roomId" column="room_id"/>
     <property name="roomName" column="room_name"/>
     <property name="numberOfSeats" column="number_of_seats"/>
     <property name="roomActive" column="room_active"/>
     <property name="reservationsList" column="room_id"
               mapped-statement="getReservationsByRoomId"/>
  </result-map>
  
  <result-map name="get-room-reservation"
              class="entities.RoomReservation">
     <property name="reservationId" column="reservation_id"/>
     <property name="roomId" column="room_id"/>
     <property name="reservationStart" column="reservation_start"/>
     <property name="reservationEnd" column="reservation_end"/>
     <property name="reservationTeam" column="reservation_team"/>
  </result-map>
  
  <mapped-statement name="getConferenceRoomById"
                    result-map="get-conference-room">
    SELECT * 
    FROM conference_rooms
    WHERE room_id = #value#; 
  </mapped-statement>
    
   <mapped-statement name="getReservationsByRoomId"
                     result-map="get-room-reservation">
    SELECT * 
    FROM room_reservations 
    WHERE room_id = #value#; 
  </mapped-statement>

(JAVA)

ConferenceRoom roomWithReservations = 
    (ConferenceRoom) sqlMap.executeQueryForObject(
       "getConferenceRoomById", new Integer(confRoom.getRoomId()));

Okay, so this one is a little more complex. But there really is not that much to it. Two result maps are used, one for ConferenceRoom and one for RoomReservations. Two mapped statements populate the beans by referencing those result maps. Obtaining the reservations is made possible by the following line in the get-conference-room result map:

<property name="reservationsList" column="room_id"
          mapped-statement="getReservationsByRoomId"/>

SQL Maps looks to the ConferenceRoom JavaBean for a List by the name of reservationsList. If the List property is there, SQL Maps uses the getReservationsByRoomId statement to populate it.

The SqlMap class also contains some convenience methods that return Java Collections. To retrieve a List of RoomReservations, without the ConferenceRoom bean, we can do this:

(SQL MAP)

<mapped-statement name="getReservationsList"
                  result-map="get-room-reservation">
  SELECT * 
    FROM room_reservations; 
</mapped-statement>    

(JAVA)

List reservationsList =
  sqlMap.executeQueryForList("getReservationsList", null);

I've demonstrated a number of ways to use SQL Maps, yet I close this section with many great SQL Map features unexamined. These features include transaction support, object caching, support for dynamic querying, and the ability to extend mapped statements for maximum code reusability.

The Many Benefits of SQL Maps

If I've done my job at all, you have a fairly good idea how using SQL Maps can simplify Java data access. However, it may not be amiss at this point to list a few important benefits the SQL Maps framework provides:

  • Eliminates low-level JDBC code that is hard to read and prone to bugs
  • Provides an efficient way to map database data, once retrieved, to Java objects
  • Allows modifications to SQL configuratively, without recompiling code
  • Enables an application to switch databases, again without recompilation
  • Lets DBAs isolate and test SQL code for efficiency

With SQL Maps you can accomplish almost everything you could using JDBC, while sparing yourself the code complexity that JDBC inevitabely brings with it.

Conclusion

This article has demonstrated the basic features of SQL Maps. If you wish to work with the examples shown in this article, you can download them here. One of the best things about the iBATIS SQL Maps framework is that it is not only easy to use, but also very well documented. I encourage you to visit the iBATIS site and read the Developer's Guide to learn more.

About the Author

Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 9 years in Information Technology and is an experienced Systems Analyst, delivering solutions that involve numerous technologies, such as J2EE and .NET.

Go to page: Prev  1  2  3  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Languages & Tools Archives

Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Intel Go Parallel Portal: Translating Multicore Power into Application Performance. Learn more.
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Intel Whitepaper: Improve Security and Control of Your PCs
Walkthrough: Building a Mobile Game in Visual Studio 2008



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers