
Originally Posted by
jacobmattison
If I have a form-backing object that has a complicated object tree -- say a Person that has a Contact Info object that has an Address object that has a bunch of Strings -- it seems that the object needs to be fully populated with component objects before I can bind to it. So if I'm creating a new Person, I need to make sure it has all the component objects populated off the bat, and if I'm retrieving a Person from the database, I need to make sure that any objects that aren't populated from the database get populated with empty objects.
First question, of course -- am I correct in my assumptions above? It does seem that if I try to bind to person.contactInfo.homeAddress.street and there is no ContactInfo, I get a null pointer exception.
Second, what's the best way to initialize my object? Is there any way to initialize an object on the fly at the time of binding?
Any suggestions are appreciated!
Hello Jacob.
I think its wrong way - create object and initialize him from DB and put that in to a web layer. All object that you send to web layer must be a data units objects (may be its my mistake ?!). Try next
DataBase --> PortalDao --> PortalService -->Web Model
and sample data unit
AbstractDataUnit
Code:
public class AbstractDataUnit {
public static AbstractDataUnit newInstance(Object candidate)
throws ServiceException {
throw new NotImplementedException("Non implemented method ");
}
}
and
UserDataUnit
Code:
public class UsersDataUnit extends AbstractDataUnit implements Serializable {
private static final long serialVersionUID = -1L;
......
//person.contactInfo.homeAddress.street
private String street;
.........
public UsersDataUnit() {
}
public UsersDataUnit(String street) {
this.street = street;
}
//now getter and setter for property
........
// and newInstance
public static AbstractDataUnit newInstance(Object candidate)
throws ServiceException {
if (candidate == null) {
return new UsersDataUnit();
}
UsersDataUnit usersDataUnit = null;
try {
Users obj = (Users) candidate; //POJO
usersDataUnit = new UsersDataUnit(obj.getContactInfo.getHomeAddress.getStreet()} catch (DataAccessException e) {
throw new UnableLoadDataException(e);
}
return usersDataUnit;
}
}
and sure that:
PortalService
Code:
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public UsersDataUnit getUser()
throws ServiceException {
Users user = null;
try {
//get a user object
user = this.getPortalDao().getUser();
} catch (DataAccessException e) {
throw new UnableLoadDataException(e);
}
return (UsersDataUnit) UsersDataUnit.newInstance(user);
}
Thats all.