Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
JSP - A mailing list about Java Server Pages specification and reference
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Subject: Object Synchronization in DAO

Subject: Object Synchronization in DAO

2005-08-02       - By Jerry Osme?a

 Back
Reply:     1     2     3     4  

I would assume that the User is a data transfer object(DTO), is that correct?

The way I will do this is I will modify the DTO to be sensitive to the changes
of any of its attributes. This can be done by adding a changes attribute of
type hashmap of  Name/value pair to your dto. Then when an attribute of this
dto changes, add the name/value pair of this attribute in the hashmap. Then
during the implementation of your DAO, you pull this list of changes and use
the name and value to generate the sql accordingly.

for e.g.

Class User {
String name;
String userId;
HashMap changes;

Public User () {
    changes = new HasMap();
}

setUserId(String userId)
{
   this.userId = userId;
   fireChanges("userId", userId); <-- you can also make a condition such that
if the value is Null or the same as the old value do not trigger the change.
}

fireChanges(String name, String value)
{
    changes.put(name, value);
}

}

In your DAO implementation get the changes map and loop thru it. Based on its
name, generate the corresponding sql and bind the corresponding value.

Hope this helps.

Regards,

Jerry
Maciej Gawinecki <d299052@(protected)> wrote:
Imagine the situation from client side of DAO Design Pattern

class User {
String name;
int userID;
...
}


...
User user = UserDAO.findUser(UserID);
user.setName("Johny");
UserDAO.updateUser(user);
...

In what a way UserDAO could guess which record in DB should be updated, if
information contained in 'user' class are ambiguous, e.g. supposing there
doesn't exist any unique primary key like userID?

Any help would be appreciated,

Maciej

====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)


-- ---- ---- ---- ---- ---- -----
Start your day with Yahoo! - make it your home page

====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)
<DIV>I would assume that the User is a data transfer object(DTO), is that
correct?</DIV>
<DIV>&nbsp;</DIV>
<DIV>The way I will do this is I will modify the DTO to be sensitive to the
changes of any of its attributes. This can be done by adding a changes
attribute of type hashmap of&nbsp; Name/value pair to your dto. Then when an
attribute of this dto changes, add the name/value pair of this attribute in the
hashmap. Then during the implementation of your DAO, you pull this list of
changes and use the name and value to generate the sql accordingly.</DIV>
<DIV>&nbsp;</DIV>
<DIV>for e.g.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Class User {</DIV>
<DIV>String name;</DIV>
<DIV>String userId;</DIV>
<DIV>HashMap changes;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Public User () {</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; changes = new HasMap();</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>setUserId(String userId)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp; this.userId = userId; </DIV>
<DIV>&nbsp;&nbsp;&nbsp; fireChanges("userId", userId); &lt;-- you can also make
a condition such that if the value is Null or the same as the old value do not
trigger the change.</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>fireChanges(String name, String value)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; changes.put(name, value);&nbsp; </DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>In your DAO implementation get the changes map and loop thru it. Based on
its name, generate the corresponding sql and bind the corresponding value.</DIV
>
<DIV>&nbsp;</DIV>
<DIV>Hope this helps.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Regards,</DIV>
<DIV>&nbsp;</DIV>
<DIV>Jerry<BR><B><I>Maciej Gawinecki &lt;d299052@(protected)&gt;</I></B>
wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER
-LEFT: #1010ff 2px solid">Imagine the situation from client side of DAO Design
Pattern<BR><BR>class User {<BR>String name;<BR>int userID;<BR>...<BR>}<BR><BR>
<BR>...<BR>User user = UserDAO.findUser(UserID);<BR>user.setName("Johny");<BR
>UserDAO.updateUser(user);<BR>...<BR><BR>In what a way UserDAO could guess which
record in DB should be updated, if<BR>information contained in 'user' class are
ambiguous, e.g. supposing there<BR>doesn't exist any unique primary key like
userID?<BR><BR>Any help would be appreciated,<BR><BR>Maciej<BR><BR>============
========================================================<BR>Companion Site: http
://www.corej2eepatterns.com<BR>J2EE BluePrints: http://java.sun.com/blueprints
/corej2eepatterns<BR>List Archive: http://archives.java.sun.com/archives
/j2eepatterns-interest.html<BR>Unsubscribing: email "signoff J2EEPATTERNS
-INTEREST" to listserv@(protected)<BR></BLOCKQUOTE><p>
               <hr size=1> <a href="http://us.rd.yahoo.com/evt=34442/*http:/
/www.yahoo.com/r/hs">Start your day with Yahoo! - make it your home page </a>
====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.