保存新对象时,反应性存储库引发异常
R2dbc 不支持 插入id有值 id有值会更新。所以实现Persistable 重写isNew 保存时如果id有值,调用setAsNew 即可保存 略显繁琐
@Table('w_user')
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements Persistable {
@Id
private Long id;
private String name;
private LocalDate birthday;
private Boolean isDelete;
@Transient
private boolean isNew;
@Override
@Transient
public boolean isNew() {
return this.isNew || id == null;
}
public User setAsNew(){
this.isNew = true;
return this;
}
}
public Mono save() {
User user = User.builder().id(snowflakeIdWorker.nextId())
.birthday(LocalDate.of(1992, Month.MAY, 2))
.name('LLL')
.isDelete(false)
.build();
return userRepository.save(user.setAsNew());
}
赞0
踩0