Java Tutorial - Java Scipt : Creating the Data Transfer Object

Java Tutorial - Java Scipt :

Creating the Data Transfer Object


We will need to be able to pass user objects around between the different layers of our application. Java Beans provide a convenient means to do this. A simple Java Bean that just has get and set methods for its attributes is called a value bean and can be used as a data transfer object (or DTO). The DTO is a standard J2EE design pattern. We will use the DTO to store the state or value of a specific row in the database and pass that state between layers of the application. Although an EJB entity bean also provides access to the state, the entity bean is not convenient to pass around within the application. Also, changes to the entity beans immediately modify the database. We may want to change the value bean and confirm that those changes are valid before we update the database. The following code represents the value bean for our user table:
package com.oldfriends.dto;
public class UserDTO extends Object implements java.io.Serializable
{
private String userid = null;
private String firstName = null;
private String lastName = null;
private String mi = null;
private String maidenName= null;
private int gradYear = 0;
private String email = null;
private String loginId = null;
private String password = null;
/** Creates new BasicUserBean */
public UserDTO()
{
}
public String getFirstName()
{
return( firstName );
}
public void setFirstName( String s )
{
firstName = s;
}
public String getLastName()
{
return( lastName);
}
public void setLastName( String s )
{
lastName = s;
}
public String getMi()
{
return( mi );
}
public void setMi( String s )
{
mi = s;
}
public String getMaidenName()
{
return( maidenName );
}
public void setMaidenName( String s )
{
maidenName = s;
}
public int getGradYear()
{
return( gradYear );
}
public void setGradYear( int grad_year )
{
gradYear = grad_year;
}
public String getLoginId()
{
return( loginId );
}
public void setLoginId( String s )
{
loginId = s;
}
public String getPassword()
{
return( password );
}
public void setPassword( String s )
{
password = s;
}
public String getEmail()
{
return( email );
}
public void setEmail( String s )
{
email = s;
}
public String getUserid()
{
return userid;
}
public void setUserid( String uid)
{
userid = uid;
}
}
Once again, we have kept this simple. Normally, you might have added several constructors and functions to test for equality, generate hash values, and clone instances of the class. The userDTO class implements the serializable interface. Serializable is a marker interface that has no methods. Marker interfaces allow others classes to easily test if a class supports a particular feature. This one is provided to inform other classes that instances of this class can be
written and read from streams.