I was wondering what\'s the best way to update some fields of a dettached object using HB on Java. Specially when the object has child objects attributes. For Example (annot
After loading a Parent object, you said
Now, I only want to allow the user to update the field2 attribute of the parent
Based on Use case, you can use a UpdateableParent object
public class UpdateableParent {
private String field2;
// getter's and setter's
}
Now our Parent repository
@Repository
public class ParentRepositoryImpl implements ParentRepository {
@Autowired
private SessionFactory sessionFactory;
public void updateSpecificUseCase(UpdateableParent updateableParentObject) {
Parent parent = sessionFactory.getCurrentSession().load(Parent.class, updateableParentObject.getId());
try {
// jakarta commons takes care of copying Updateable Parent object to Parent object
BeanUtils.copyProperties(parent, updateableParentObject);
} catch (Exception e) {
throw new IllegalStateException("Error occured when updating a Parent object", e);
}
}
}
Its advantages
Although it is not a Technology-related question, Seam framework allows you update only what you want. So you do not have to worry about what pattern to use.
regards,