JPA存储库更新失败。
following code of service not works. insert works
sping boot 2.1.7
@Override
@Transactional
public Employee updateEmployee(Employee emp, long id) throws Exception {
System.out.println("Repository emp" + emp.getPlantModel() + " id " + id);
// return employeeRepository.save(emp);
Optional<Employee> employee = employeeRepository.findById(id);
System.out.println("plant make" + emp.getPlantMake());
System.out.println("emo is present" + employee.isPresent());
if (employee.isPresent()) {
Employee newEntity = employee.get();
newEntity.setPlantMake(emp.getPlantMake());
newEntity.setPlantModel(emp.getPlantModel());
System.out.println("updating employee entity");
return employeeRepository.save(newEntity);
} else {
return null;
}
}
插入有效,jpa update引发异常而没有任何重要原因
实体类如下,我已连接到postgresSQL
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "emp_id", columnDefinition = "serial")
private long empId;
@Column(name = "plant_make")
private String plantMake;
使用Spring Boot 2.17寻找解决方案