【发布时间】:2015-02-18 22:40:52
【问题描述】:
我正在更新我的数据库中的一个条目,为此我创建了一个对象,其中只有我想要更新的字段。
我的问题是数据库中的空字段也被替换了。所以当我只想忽略那些空值时,数据库中的非空值会被空值替换。
我尝试在 @Entity 中使用 @DynamicUpdate 注释,但它不起作用,因为它仍然用 null 替换那些非空值,因为它们具有不同的值。
到目前为止,这是我的代码:
@Entity
@DynamicUpdate
@Table(name = "Exer")
public class Exer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String nome;
private String grupo;
private String path;
}
在控制器中:
Exer exer = new Exer();
exer.setId(4);
exer.setNome("value");
exerDao.update(exer);
更新的dao实现:
@PersistenceContext
private EntityManager em;
@Transactional
public Exer update(Exer exer) {
return em.merge(exer);
}
在这种情况下,我只想将 Nome 更改为“值”,而是为此条目的其他两个字段插入 null。
【问题讨论】:
标签: java mysql hibernate jpa annotations