advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Make the Correct Data Classes in Your DAO Applications :

Database Views

Data is often read from a database using a view. A view is a SELECT statement joining several tables most often using a WHERE clause. It's absolutely legal to make a DAO that maps a view. Since a view cannot be used for updating a database, such a DAO will only contain finder-methods.

The Transaction Handler Layer

Having determined to use "DAO beans" to communicate with the DAO layer, it's now time to see how clients should communicate with the transaction handler layer. First of all, the API offered by the handlers should be useful to the clients. This means that method parameters should match the data the clients have at hand. Clients can be browser/Swing applications or Web services, but they may also be other transaction handlers. Therefore, expect the handlers to have several APIs available.

The picture now looks like this:

A DTO is often a mixture of data returned from more than one DAO bean, i.e. data held in several tables on the database. If you expand your "DVD business case" with a Director table, then you could create a client demand that fetched DVD information—including director data (name, birthday, other movies, etc.). Other clients might want only the title of a DVD; it's not difficult to imagine many client requests that differ only slightly in the amount of data they want returned.

Here's an example: suppose the DVD table contains a key to the Director table—should you return only the key? Or should you return some or all of the director data? And if the Director table has keys to other tables, when should you stop retrieving and returning data to the clients?

If you're not careful, you may end up with a lot of DTOs, all having a lot of properties in common, but without proper inheritance. There is no single, simple answer to how to cut your DTOs correctly, but there are some guidelines which have proven useful.

Using the Same Data Classes

You may be tempted to use the same data classes as transport to the handlers and the DAOs. In simpler applications, or when handling simple requests, the clients want exactly what's in the DAO beans. The downside to doing this is that changes in a DAO&3151;and thus in the DAO bean—will be visible to the clients. To avoid tight bindings between the layers, only use the DAO beans as the interface to the DAOs.

You may have noticed, that in the previous articles about the DAO patterns, DAO beans were also used as DTOs. This was done to keep the focus on the application layers, and not on the data transport.

Returning Only the Key

Some clients may only want the database key returned. Therefore, a good idea is to define a Key class for the DTOs—and also for the DAO beans—and let all DTOs use this class (assuming, for simplicity, that all DTOs use the same key type).
package dk.hansen.dto;

public class Key_DTO {

  private String id;

  public Key_DTO() {}

  public Key_DTO(String id) {
    this.id =id;
  }

  public String getId() {return id;}

  public void setId(String id) {this.id = id;}
}

Using the Key

The DTO with DVD information may now use Key_DTO:
package dk.hansen.dto;

public class DVDKey_DTO {

  private Key_DTO key;

  public Key_DTO getKey() {return key;}

  public void setKey(Key_DTO key) {this.key = key;}
. . .
The smallest possible DVD DTO contains only one property besides the key:
package dk.hansen.dto;

public class DVDKeyValue_DTO extends DVDKey_DTO {

  private String title;

  public String getTitle() {return title;}

  public void setTitle(String title) {this.title = title;}

  public static void main(String[] args) {
    DVDKeyValue_DTO dvd = new DVDKeyValue_DTO();
    dvd.setKey(new Key_DTO("12"));
    dvd.setTitle("Jurassic Park");
  }
}
The main method merely illustrates how a handler could use the DTO.

As long as you can extend one class into another, everything is fine. But, of course, you can imagine a DVD DTO with any combination of properties from the DVD bean, and you'd never make a DTO for every combination. What often is useful is to create:

  • A DTO with only a key.
  • A DTO with the key and an important property, like the title of the DVD (often used in GUI drop downs).
  • A DTO with all the properties from the DVD
You can use the above scheme to make these classes.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans
NetBeans, OpenSolaris Also in Spotlight at JavaOne

Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")
Comparing Multi-Core Processors for Server Virtualization
Intel® Desktop Business Computing Solutions
Intel: What Downturn?
Managing the Evolving Data Center
Implement Drag and Drop in Your Windows Forms Applications
Processing Linked Web Data with XSLT

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



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

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES