DAO pattern Confuse

Hi all,

I have 4 tables deal with 4 DAOs. But I have a funcion called search() that get infomation from 4 tables above. Will I put this function in which DAO ?.

I see each DAO work only on self table. such as searchBy..., delete, update. But a query complex we will put in which DAO ?

Thanks

[316 byte] By [musica] at [2007-10-2 20:56:29]
# 1

> Hi all,

> I have 4 tables deal with 4 DAOs.

Usually DAOs map to objects, not tables, unless your objects also happen to map 1:1 with the tables.

> But I have a

> funcion called search() that get infomation from 4

> tables above. Will I put this function in which DAO

> ?.

If a single object can have JOINed information from four tables, with objects as data members, then you should have one object with the others as children and only write one DAO - the one for the parent.

If you were doing object/relational mapping, there would be one DAO and mappings for the 1:1, 1:m, and m:n relationships between the parent and its children.

> I see each DAO work only on self table. such as

> searchBy..., delete, update. But a query complex we

> will put in which DAO ?

You can have a separate Finder class that has complex queries if you need them.

%

duffymoa at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Or you can have view which joins the required columns(searchable columns) and write a DAO or that View.
ssv45324a at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Hi,

<<Will I put this function in which DAO ?>>

Your answer is no! DAO pattern is designed to solving jdbc spaghetti codes. and you did! what you need is not query from tables but is query from objects that you shoud implement this search in your bussiness logic. you can use "Business Object" pattern.

Regrads

Sarah_Mahdavia at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
duffywhat do u mean by "Usually DAOs map to objects, not tables, unless your objects also happen to map 1:1 with the tables."I thought you should have a DAO for every table also, for example a DAO for employee , DAO for user etc can u clarify please
a_suna at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Hi,

I mean : I have 4 table : A,B,C,D

and I wrote : ADAO, BDAO,CDAO,DDAO for each table

And now I have a query string like :

"select x,y,z

form A

INNER JOIN B ON ...

LEFT JOIN C ON...

LEFT JOIN D ON .."

wil I put this query in which DAO ? (ADAO, BDAO, CDAO,or DDAO ?)

musica at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

> Hi,

> <<Will I put this function in which DAO ?>>

> Your answer is no! DAO pattern is designed to solving

> jdbc spaghetti codes. and you did! what you need is

> not query from tables but is query from objects that

> you shoud implement this search in your bussiness

> logic. you can use "Business Object" pattern.

> Regrads

I use a DAO per table. Thats what my DAOs are for. If one table changes I don't want to change 2-3 DAOs, just one.

I prefer this logic in a special DAO though. If you put it in a Business object, then you loose all the power of SQL.

Which in the end means if a table changes I have to change the appropriate DAO, plus any additional special DAOs as well. To be honest though I tend to mix this into one of the existing DAOs.

_dnoyeBa at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> > Hi,

> > <<Will I put this function in which DAO ?>>

> > Your answer is no! DAO pattern is designed to

> solving

> > jdbc spaghetti codes. and you did! what you need

> is

> > not query from tables but is query from objects

> that

> > you shoud implement this search in your bussiness

> > logic. you can use "Business Object" pattern.

> > Regrads

>

> I use a DAO per table. Thats what my DAOs are for.

> If one table changes I don't want to change 2-3

> DAOs, just one.

>

> I prefer this logic in a special DAO though. If you

> put it in a Business object, then you loose all the

> power of SQL.

>

Not to mention the speed.

jschella at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

> Hi all,

> I have 4 tables deal with 4 DAOs. But I have a

> funcion called search() that get infomation from 4

> tables above. Will I put this function in which DAO

> ?.

> I see each DAO work only on self table. such as

> searchBy..., delete, update. But a query complex we

> will put in which DAO ?

>

If you want a pattern for this then the Java Blueprints specifically has one for it.

If it was me I would just stick it someplace convenient though.

jschella at 2007-7-13 23:41:04 > top of Java-index,Other Topics,Patterns & OO Design...