Java 如何更新jpa中的实体?
我是按照这样做的。这让我感到恶心Java 如何更新jpa中的实体?,java,jpa,optimization,Java,Jpa,Optimization,我是按照这样做的。这让我感到恶心 public class CounselInfoServiceImpl extends BaseServiceImpl<CounselInfoDao, CounselInfoEntity, Long> implements CounselInfoService { @Inject ClassService classService; @Inject @Override public void
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;
}
}
它提到了分离的对象,但更新模型只是得到了一个日期。
所以,如果我合并模型,模型的所有值都将应用于实体。密码,我不想更新它,并且editTo不应该触碰密码。现在,更新看起来像这样
public CounselInfoEntity editTo(CounselInfoEntity model)
{
CounselInfoEntity entity = id(model.getId());
List<? extends Attribute<CounselInfoEntity, ?>> editAttrs = Lists.<Attribute<CounselInfoEntity, ?>>newArrayList(CounselInfoEntity_.name,
CounselInfoEntity_.address,
CounselInfoEntity_.education,
CounselInfoEntity_.phone,
CounselInfoEntity_.QQ,
CounselInfoEntity_.remark,
CounselInfoEntity_.school,
CounselInfoEntity_.age,
CounselInfoEntity_.sex);
BeanHelper.merge(entity, model, BeanHelper.skipNullOrEmpty(model, editAttrs));
if (model.getClassIntention() != null)
{
entity.setClassIntention(classService.id(model.getClassIntention().getId()));
}
return entity;
}
这个更好,因为
这是类型安全的
易于筛选/选择要合并的属性
包括
中期
commons-lang3为什么是小海狸?看见
我尽力了,这是我能做的最好的了。你有一种奇怪的做法。通常,当使用应用程序编辑实体时,用户使用包含实体所有字段的预填充表单,修改必须修改的字段,提交此表单,所有字段(无论是否为空)都写入实体。我从未见过这样的表单:将字段保留为空意味着不要修改此属性的值。表单中没有空字段,因为我不会显示这些字段。某些文件是验证敏感的。因此,用户只能编辑整个日期的一部分。我不明白这意味着什么。你说的是哪一天?表格是什么样子的?verify sensitive是什么意思?表单不会显示所有日期,因此,模型不会获取所有日期。更新时,我必须测试修改了哪些部分。但是,有时,即使模型中的日期发生了更改,我也不应该对实体进行更改,例如。“版本”字段。我会找到其他方法,在我理清思路后发布。
public CounselInfoEntity editTo(CounselInfoEntity model)
{
CounselInfoEntity entity = id(model.getId());
List<? extends Attribute<CounselInfoEntity, ?>> editAttrs = Lists.<Attribute<CounselInfoEntity, ?>>newArrayList(CounselInfoEntity_.name,
CounselInfoEntity_.address,
CounselInfoEntity_.education,
CounselInfoEntity_.phone,
CounselInfoEntity_.QQ,
CounselInfoEntity_.remark,
CounselInfoEntity_.school,
CounselInfoEntity_.age,
CounselInfoEntity_.sex);
BeanHelper.merge(entity, model, BeanHelper.skipNullOrEmpty(model, editAttrs));
if (model.getClassIntention() != null)
{
entity.setClassIntention(classService.id(model.getClassIntention().getId()));
}
return entity;
}
package me.wener.practices.web.common.util;
import com.google.common.collect.Lists;
import java.lang.reflect.Field;
import java.util.List;
import javax.persistence.metamodel.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
@Slf4j
public class BeanHelper
{
/**
* 获取 bean 的属性,如果属性不存在或发生异常返回null
*/
public static Object tryGetProperty(Object bean, String attrName)
{
Object property = null;
try
{
Field field = FieldUtils.getField(bean.getClass(), attrName, true);
property = field.get(bean);
} catch (Exception e)
{
if (log.isErrorEnabled())
log.error("Exception when get property " + attrName + " on " + bean, e);
}
return property;
}
public static <T, A extends Attribute<T, ?>> Object tryGetProperty(T bean, A attr)
{
return tryGetProperty(bean, attr.getName());
}
public static <T, A extends Attribute<T, ?>> boolean trySetProperty(T bean, A attr, Object value)
{
return trySetProperty(bean, attr.getName(), value);
}
public static boolean trySetProperty(Object bean, String attrName, Object value)
{
boolean failed = false;
try
{
// 对于 chain 的 setter 方法, 必须要使用 force access.
Field field = FieldUtils.getField(bean.getClass(), attrName, true);
field.set(bean, value);
} catch (Exception e)
{
if (log.isErrorEnabled())
log.error("Exception when set property " + attrName + " on " + bean, e);
failed = true;
}
return !failed;
}
/**
* Test the value of search in attrs is make the isNull and isEmpty
* <p/>
* isEmpty will apply when value is String
*/
@SafeVarargs
public static <E, A extends Attribute<E, ?>> List<A> skip(Object searcher, boolean skipNull, boolean skipEmpty, A... attrs)
{
return skip(searcher, skipNull, skipEmpty, Lists.newArrayList(attrs));
}
public static <E, A extends Attribute<E, ?>> List<A> skip(Object searcher, boolean skipNull, boolean skipEmpty, List<A> attrs)
{
List<A> list = Lists.newArrayList();
boolean valid;
for (A attr : attrs)
{
Object value = tryGetProperty(searcher, attr.getName());
valid = skipNull || value != null;
if (valid && skipEmpty && value instanceof String)
valid = ((String) value).length() != 0;
if (valid)
list.add(attr);
}
return list;
}
@SafeVarargs
public static <E, A extends Attribute<E, ?>> List<A> skipNullOrEmpty(Object searcher, A... attrs)
{
return skip(searcher, true, true, attrs);
}
public static <E, A extends Attribute<E, ?>> List<A> skipNullOrEmpty(Object searcher, List<A> attrs)
{
return skip(searcher, true, true, attrs);
}
@SafeVarargs
public static <T, A extends Attribute<T, ?>> T merge(T target, T src, A... attrs)
{
return merge(target, src, Lists.newArrayList(attrs));
}
public static <T, A extends Attribute<T, ?>> T merge(T target, T src, List<A> attrs)
{
for (A attr : attrs)
{
String attrName = attr.getName();
Object value = tryGetProperty(src, attrName);
trySetProperty(target, attrName, value);
}
return target;
}
}