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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 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%.

Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'

C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization
Microsoft Shows Some Ankle With Visual Studio
Top 5 Reasons to Adopt SQL Server 2008

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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES