Я использую Spring JPA для своей базы данных. После обновления данных в базе данных извлекаются все данные из базы данных, но одна запись, которую я обновил, возвращает эту запись из памяти.
Контроллер
@RestController
@Slf4j
public class DepartmentController {
@PutMapping("${service.deptupdate.url}")
@ApiOperation(value = "Update existing department by deptId and clientId", response = Boolean.class)
public ResponseEntity<List<DepartmentDTO>> updateDept(@RequestBody DepartmentDTO deptDTO)
throws DepartmentException {
log.info("DepartmentController : updateDept: saving the department {} for {} client", deptDTO.getDeptId(),
deptDTO.getClientId());
deptDTO = departmentService.saveOrUpdateDept(deptDTO);
log.info("DepartmentController : updateDept: Successfully updated department {} for clinet{}",
deptDTO.getDeptId(), deptDTO.getClientId());
return new ResponseEntity<List<DepartmentDTO>>(departmentService.findAllDepartment(deptDTO.getClientId()),
HttpStatus.OK);
}
}
Сервис
@Slf4j
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Override
public DepartmentDTO saveOrUpdateDept(DepartmentDTO deptDTO) {
log.info("Saving department for clientId{}", deptDTO.getClinicId());
Department dept = DepartmentUtils.deptDetailsDTOTOEntity(deptDTO);
dept = departmentRepository.saveAndFlush(dept);
DepartmentDTO departmentDTO = DepartmentUtils.deptDetailsEntityTODTO(dept);
if(null != departmentDTO.getDeptId())
log.info("Successfully save/update department {} for client{}", departmentDTO.getDeptId()+":"+departmentDTO.getDeptName(), departmentDTO.getClientId());
return departmentDTO;
}
@Override
public List<DepartmentDTO> findAllDepartment(Long clientId) {
log.info("Getting all department list for clientId{}", clientId);
List<Department> deptList = departmentRepository.findByClientId(clientId);
List<DepartmentDTO> deptDTOList = DepartmentUtils.deptDetailsEntityTODTOList(deptList);
log.info("Found {} department list for clientId{}", deptDTOList.size(), clientId);
return deptDTOList;
}
}
Хранилище
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long>{
public List<Department> findByClientId(@Param("cliId")Long clientId);
}
Сущность
@Data
@Entity
@Table(name = "DEPARTMENT")
@DynamicUpdate(true)
@DynamicInsert(true)
@EqualsAndHashCode
public class Department implements Serializable{
@Id
@Column(name = "DEPT_ID", updatable=false, nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long deptId;
@Column(name = "CLIENT_ID", nullable = false, updatable = false)
private Long clientId;
@Column(name = "DEPT_NAME")
private String deptName;
@Column(name = "DEPT_CODE")
private String deptCode;
@Column(name = "STATUS")
private Boolean status;
@Column(name = "CREATED_BY", nullable = false, updatable = false)
private String createdBy;
@CreationTimestamp
@Column(name = "CREATED_TIME", nullable = false, updatable = false)
private LocalDateTime createdTime;
@Column(name = "UPDATED_BY")
private String updatedBy;
@UpdateTimestamp
@Column(name = "UPDATED_TIME", nullable = false)
private LocalDateTime updatedTime;
}
Ниже Конфигурация моей базы данных в файле yml
# Database Configuration
spring:
application:
name: eclinic-queue-management
profile:
active: local
datasource:
url: jdbc:mysql://localhost:3306/client?useSSL=false&serverTimezone=UTC
username: root
password: *****
driver.class: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate.format_sql: true
hibernate.ddl-auto: none
Обновление данных ниже через RESTAPI - Запрос
{
"deptId": 1,
"clientId" : 2,
"deptName" : "General",
"deptCode" : "GGG",
"status": true,
"createdBy": "GULAB",
"updatedBy": "GULAB"
}
Возврат ответа из базы данных
[
{
"deptId": 1,
"clinicId": 2,
"deptName": "General",
"deptCode": "GGG",
"deptDoctorId": "6",
"tokenStartWith": "GGG",
"status": true,
"createdBy": "GULAB", // <-- this is not the value from the database
"createdTime": null, // <-- this is not the value from the database
"updatedBy": "GULAB",
"updatedTime": "2020-04-02T22:21:11.352"
},
{
"deptId": 2,
"clinicId": 2,
"deptName": "General",
"deptCode": "GEN",
"deptDoctorId": "3",
"tokenStartWith": "G",
"status": true,
"createdBy": "Jayesh",
"createdTime": "2020-03-27T12:42:50.076",
"updatedBy": "Jayesh",
"updatedTime": "2020-03-27T12:42:50.077"
}
]
Но в базе данных createdBy
значение Jayesh
и createdTime
не null
Я не понимаю, в чем проблема.