aspose file tools*
The moose likes Object Relational Mapping and the fly likes Relationship between tables in DAO pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Relationship between tables in DAO pattern" Watch "Relationship between tables in DAO pattern" New topic
Author

Relationship between tables in DAO pattern

Huy Hang
Greenhorn

Joined: Jul 22, 2005
Posts: 5
I use DAO pattern.
I wanna get data from 4 tables, which dto will I use ?
Create new dto content all attribute I need ?
and create new DAO interface, DAO implements class ?
Thanks for rely !
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10206
    
166

If the 4 tables represent 4 INDEPENDENT entities, i believe you should create separate DTOs for each of them.
Ex:
Tables:
CONTINENT ( Name varchar , poupulation Integer);
COUNTRY (Name varchar, poulation Integer, Number literacyRate);
VILLAGE (Name varchar, population Integer, Number literacyRate);

You would have DTOs as follows:

class ContinentDTO {
String name;
int population;
//This would be a collection of Country DTO
Collection countries;


}

class CountryDTO {
String name;
int population;
double literacyRate;
//This would be a collection of Village DTO
Collection villages;

//this would refer to the continent DTO to which this country belongs
ContinentDTO continent;


}

class VillageDTO {
String name;
int population;
double literacyRate;

//this would refer to the country to which this village belongs
CountryDTO country;
}


[My Blog] [JavaRanch Journal]
Huy Hang
Greenhorn

Joined: Jul 22, 2005
Posts: 5
That is a good ideal but I wanna get data from 4 tables so that Object return contents attributes exist in 4 tables. Will i create new dto object
Example :
Have 4 tables : A, B, C, D with attribute A.a, B.b, C.c, D.d
"select A.a, B.b, C.c, D.d where ...."
which DAO object I can put in ?
Jack Wiesenthaler
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
Originally posted by jaikiran pai:
If the 4 tables represent 4 INDEPENDENT entities, i believe you should


Who told you this? The purpose of a Transfer object is to transfer data between the data access layer and the client requesting the data. The Transfer Object has nothing to do with entities! A Transfer Object can simply be used to represent the data returned by the consolidation of many remote calls into a single call.

I think you should read the Design Pattern definition again ;-)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10206
    
166

Sorry, my thinking was wrong.
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

You should create 4 DAOs for for 4 tables and probably a single DTO with the help of Data Transfer Object Assembler.

-Jignesh
Huy Hang
Greenhorn

Joined: Jul 22, 2005
Posts: 5
I agree in DAO pattern 1 Table map to a DAO. But my query section get data from 4 tables. How can u do ?
Pj Murray
Ranch Hand

Joined: Sep 24, 2004
Posts: 194
Hello Huy,

You appear to be using FireStorm/DAO. You could look at the FAQ

http://www.codefutures.com/products/firestorm/faq/jdbc/complexqueries.html


FireStorm/DAO supports Table DAOs, View DAOs, Stored Procedure DAOs, and Custom SQL DAOs. Custom SQL DAOs can be used to expose pretty much any SQL statement as a DAO object (multi-table joins, bulk updates, etc.)


PJ Murray -
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 78
PJ

FAQ does not say how we can relate result columns to getter/setter methods.

As an example if we have the first query:

SELECT a.*, b.* FROM a, b WHERE a.id = b.id AND b.install_date between ? and ?

How are we going to name setter/getter methods? (we may have fields with the same column name coming from different tables).

I am not able to find the answer for this.

Would you please forward me to a tutorial or a reference?

Siamak


PhD Artificial Intelligence, OCJP1.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Relationship between tables in DAO pattern