I am doing it following way. It's making me feel sick!
public class CounselInfoServiceImpl
extends BaseServiceImpl<CounselInfoDao, CounselInfoEntity, Long>
implements CounselInfoService {
@Inject
ClassService classService;
@Inject
@Override
public void setDao(CounselInfoDao dao)
{
super.setDao(dao);
}
@Override
public CounselInfoEntity editTo(CounselInfoEntity model)
{
CounselInfoEntity entity = id(model.getId());
if (!Strings.isNullOrEmpty(model.getName()))
{
entity.setName(model.getName());
}
if (!Strings.isNullOrEmpty(model.getAddress()))
{
entity.setAddress(model.getAddress());
}
if (!Strings.isNullOrEmpty(model.getEducation()))
{
entity.setEducation(model.getEducation());
}
if (!Strings.isNullOrEmpty(model.getPhone()))
{
entity.setPhone(model.getPhone());
}
if (!Strings.isNullOrEmpty(model.getQQ()))
{
entity.setQQ(model.getQQ());
}
if (!Strings.isNullOrEmpty(model.getRemark()))
{
entity.setPhone(model.getPhone());
}
if (!Strings.isNullOrEmpty(model.getSchool()))
{
entity.setSchool(model.getSchool());
}
if (model.getAge() != null)
{
entity.setAge(model.getAge());
}
if (model.getSex() != null)
{
entity.setSex(model.getSex());
}
if (model.getClassIntention() != null)
{
entity.setClassIntention(
classService.id(
model.getClassIntention().getId()));
}
return entity;
}
}
Any suggestions to avoid this spaghetti code
?
BTW, writing this code is a hard work!
EDIT
BTW, I don't think the em.merge
is ready for this. See here
The EntityManager.merge() operation is used to merge the changes made to a detached object into the persistence context.
It mentioned the detached object
, but the update model just got a piece of date.
So, if I merge the model, all of model's value will apply to entity.(e.g. password, which I don't want to update, and the editTo
should not touch the password.)