使用 Spring 数据刷新后加载完整对象
Load complete object after refresh with Spring Data
我在 运行 更新后加载对象的所有属性时遇到问题。我的项目正在使用 Spring 引导和 Spring 数据。
我的 class 配置如下:
@Entity
@Table(name = "clientes")
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column(name = "cadastro", nullable = false, updatable = false)
private LocalDate cadastro;
private String nome;
private String ramoDeAtuacao;
// get() e set()
}
我的存储库:
@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Long> {
}
请注意,当我使用 Save 方法创建新的 Cliente 时,注册日期只会生成一次。 此操作发生在我的服务中:
@Service
public class ClienteServiceImpl implements ClienteService {
private ClienteRepository repository;
@Autowired
public ClienteServiceImpl(ClienteRepository repository) {
this.repository = repository;
}
@Override
public Cliente save(Cliente cliente) {
cliente.setCadastro(LocalDate.now());
return repository.save(cliente);
}
@Override
public Cliente update(Cliente cliente) {
return repository.save(cliente);
}
}
我的控制器:
@RestController
@CrossOrigin
@RequestMapping(path = "/clientes")
public class ClienteController {
@Autowired
private ClienteService service;
@PutMapping
public ResponseEntity<?> atualiza(@RequestBody Cliente cliente) {
Cliente clienteAtualizado = service.atualiza(cliente);
return new ResponseEntity<Cliente>(clienteAtualizado, HttpStatus.OK);
}
}
这是为创建新客户端而发送的JSON。
{
"nome": "Thiago",
"ramoDeAtuacao": "T.I."
}
returned JSON的注册日期是这样的:
{
"id": 1,
"cadastro": "20/07/2019",
"nome": "Thiago",
"ramoDeAtuacao": "T.I."
}
现在,当我尝试升级时,我无法return注册日期。这是更新 JSON:
{
"id": 1,
"nome": "Thiago MF",
"ramoDeAtuacao": "Desenvolvedor"
}
returned JSON 没有注册日期和 returns NULL:
{
"id": 1,
"cadastro": null,
"nome": "Thiago MF",
"ramoDeAtuacao": "Desenvolvedor"
}
如何解决?
您的更新查询没有日期字段,因此数据库中的现有字段将更新为空。
如果您只需要更新日期字段以外的字段,那么更新函数应该类似于
@Override
public Cliente update(Cliente cliente) {
Cliente c = repository.findOne(id);
// Now set the values to be updated on c from cliente
return repository.save(c);
}
我在 运行 更新后加载对象的所有属性时遇到问题。我的项目正在使用 Spring 引导和 Spring 数据。 我的 class 配置如下:
@Entity
@Table(name = "clientes")
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column(name = "cadastro", nullable = false, updatable = false)
private LocalDate cadastro;
private String nome;
private String ramoDeAtuacao;
// get() e set()
}
我的存储库:
@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Long> {
}
请注意,当我使用 Save 方法创建新的 Cliente 时,注册日期只会生成一次。 此操作发生在我的服务中:
@Service
public class ClienteServiceImpl implements ClienteService {
private ClienteRepository repository;
@Autowired
public ClienteServiceImpl(ClienteRepository repository) {
this.repository = repository;
}
@Override
public Cliente save(Cliente cliente) {
cliente.setCadastro(LocalDate.now());
return repository.save(cliente);
}
@Override
public Cliente update(Cliente cliente) {
return repository.save(cliente);
}
}
我的控制器:
@RestController
@CrossOrigin
@RequestMapping(path = "/clientes")
public class ClienteController {
@Autowired
private ClienteService service;
@PutMapping
public ResponseEntity<?> atualiza(@RequestBody Cliente cliente) {
Cliente clienteAtualizado = service.atualiza(cliente);
return new ResponseEntity<Cliente>(clienteAtualizado, HttpStatus.OK);
}
}
这是为创建新客户端而发送的JSON。
{
"nome": "Thiago",
"ramoDeAtuacao": "T.I."
}
returned JSON的注册日期是这样的:
{
"id": 1,
"cadastro": "20/07/2019",
"nome": "Thiago",
"ramoDeAtuacao": "T.I."
}
现在,当我尝试升级时,我无法return注册日期。这是更新 JSON:
{
"id": 1,
"nome": "Thiago MF",
"ramoDeAtuacao": "Desenvolvedor"
}
returned JSON 没有注册日期和 returns NULL:
{
"id": 1,
"cadastro": null,
"nome": "Thiago MF",
"ramoDeAtuacao": "Desenvolvedor"
}
如何解决?
您的更新查询没有日期字段,因此数据库中的现有字段将更新为空。
如果您只需要更新日期字段以外的字段,那么更新函数应该类似于
@Override
public Cliente update(Cliente cliente) {
Cliente c = repository.findOne(id);
// Now set the values to be updated on c from cliente
return repository.save(c);
}