Discussions

General J2EE: DAO Pattern

  1. DAO Pattern (3 messages)

    Hi


    I have some questions about the DAO Pattern.

    Say that I have a database with one table named Persons (PersonId, name), one table named Groups (GroupId, GroupName) and one table GroupMemberships (id, GroupId, PersonId).

    Persons and Groups are in the java app represented by the classes Person and Group.

    Using the DAO pattern, should the data access code for these objects be in seperate DAOs, i.e. PersonDAO and GroupDAO?

    If one wants to retrieve all persons in a specified group (sql something likes this: select PersonId, Name from Persons as p, GroupMemberships as m where m.GroupId = ? and p.PersonId = m.PersonId), in which DAO should that code be?

    Threaded Messages (3)

  2. DAO Pattern[ Go to top ]

    I think this it is more then one way possible.
    i like to code one dao for every sql-statement. other people create one dao for all statements over one table.

    if you code one dao for every sql-query (forgett the class-overhead) it simplier to create a dao-cache or other helper-classes.
    cause every dao have only to methods:

    VO getByPrimaryKey( Serializable id);
    Iterator getAll();
    Iterator getByValueKey( String name, Object value);

    a better way is, if you configure the dao before you get the values.
    so you develope a dao like a sql-pattern with settable values.

    sorry, i can't speak english. this are first steps in english ;)
  3. DAO Pattern[ Go to top ]

    Unless you create a GroupMembershipDAO, it makes sense for it to be a method on the PersonDAO...since your returning a collection of Person Objects.

    Collection people = personDAO.findByGroupdId(int group_id);
  4. DAO Pattern[ Go to top ]

    Instead of having a seperate DAO for each sql statement, it might be good to have related sql statements as part of that particular DAO. Sometimes these SQL statements could belong to more than one table, where all these SQL statements tries to address similar/common business needs

     For ex:
     All persons related queries as part of PersonsDAO and all group related queries as GroupDAO.

     I think, having a seperate DAO for each SQL statement adds more complexity than the problems it tries to address.