使用最简单的方法进行update操作
public Merchant modifyOne(Merchant merchant) {
return dao.save(merchant);
}
传递进来的方法参数merchant,已经设置了id和一部分属性,其他属性没有做设置(即null)
控制台中的日志中显示的是先select,再update
Hibernate: select merchant0_.ID as ID1_2_0_, ......
Hibernate: update MERCHANT set ADDRESS=?, ......
但实际update操作时,并没有把select出来的结果和方法中传递进来的参数merchant进行合并, 提示 ADDRESS can not be null
实体上也试过加上DynamicUpdate之类,但没有作用
@Entity
@DynamicInsert
@DynamicUpdate
public class Merchant implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, length = 200)
private String address;
......
}
难道一定要在更新调用save之前,手工findOne一次,将查询结果和参数手工合并么?
求解。。。
查了好久,确实如 @好怕麻烦 所说,是这个样子。
至于为啥@DynamicUpdate无效还不大清楚。。。
目前能够找到的解决方案是实现自己的JpaRepository
这里给出自己的实现以作参考(springboot 1.5.6测试通过)
定义自己的ExtJpaRepository接口
接口ExtJpaRepository的实现(其中getNullPropNames为自定义方法)
实现自己的ExtJpaRepositoryFactoryBean以替代原有的JpaRepositoryFactoryBean
ExtJpaRepositoryFactoryBean中所用到的ExtJpaRepositoryFactory
最后,入口Application中指定使用自定义的FactoryBean