The patient registration class has patient id which is unique. the id should be like – P2024070280001, P is prefix, 2024 is the current year, 07 means the current month, 028 means that specific day, 0001 means the serial, first patient of that day.
If 200 patients register today, it will be P2024070280200. The next day, if a new patient is registered at the beginning of the day, the serial would be P2024070290001. Every time a patient is registered, the serial will auto generate and increase. Is this logic sufficient for Unique Id generation? How to do it in a robust and efficient way?
<code>import lombok.Data;
import java.time.LocalDate;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private String patientId;
<code>import lombok.Data;
import java.time.LocalDate;
@Entity
@Data
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String patientId;
private String name;
private Integer age;
private String address;
private String contact;
}
</code>
import lombok.Data;
import java.time.LocalDate;
@Entity
@Data
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String patientId;
private String name;
private Integer age;
private String address;
private String contact;
}
This is the service class
<code>import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
public class PatientService {
private final PatientRepository patientRepository;
public PatientService(PatientRepository patientRepository) {
this.patientRepository = patientRepository;
public Patient savePatient(Patient patient) {
String patientIdPrefix = "P";
LocalDate currentDate = LocalDate.now();
String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String serialNumber = generateSerialNumber(currentDate);
patient.setPatientId(patientIdPrefix + formattedDate + serialNumber);
return patientRepository.save(patient);
public List<Patient> getAllPatients() {
return patientRepository.findAll();
public Optional<Patient> getPatientById(Long id) {
return patientRepository.findById(id);
public Patient updatePatient(Patient patient) {
return patientRepository.save(patient);
public void deletePatient(Long id) {
patientRepository.deleteById(id);
private String generateSerialNumber(LocalDate currentDate) {
String formattedSerial = String.format("%04d", serial++);
<code>import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
@Service
public class PatientService {
private final PatientRepository patientRepository;
@Autowired
public PatientService(PatientRepository patientRepository) {
this.patientRepository = patientRepository;
}
public Patient savePatient(Patient patient) {
// Generate patient ID
String patientIdPrefix = "P";
LocalDate currentDate = LocalDate.now();
String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String serialNumber = generateSerialNumber(currentDate);
patient.setPatientId(patientIdPrefix + formattedDate + serialNumber);
return patientRepository.save(patient);
}
public List<Patient> getAllPatients() {
return patientRepository.findAll();
}
public Optional<Patient> getPatientById(Long id) {
return patientRepository.findById(id);
}
public Patient updatePatient(Patient patient) {
return patientRepository.save(patient);
}
public void deletePatient(Long id) {
patientRepository.deleteById(id);
}
private String generateSerialNumber(LocalDate currentDate) {
static int serial = 1;
String formattedSerial = String.format("%04d", serial++);
return formattedSerial;
}
}
</code>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
@Service
public class PatientService {
private final PatientRepository patientRepository;
@Autowired
public PatientService(PatientRepository patientRepository) {
this.patientRepository = patientRepository;
}
public Patient savePatient(Patient patient) {
// Generate patient ID
String patientIdPrefix = "P";
LocalDate currentDate = LocalDate.now();
String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String serialNumber = generateSerialNumber(currentDate);
patient.setPatientId(patientIdPrefix + formattedDate + serialNumber);
return patientRepository.save(patient);
}
public List<Patient> getAllPatients() {
return patientRepository.findAll();
}
public Optional<Patient> getPatientById(Long id) {
return patientRepository.findById(id);
}
public Patient updatePatient(Patient patient) {
return patientRepository.save(patient);
}
public void deletePatient(Long id) {
patientRepository.deleteById(id);
}
private String generateSerialNumber(LocalDate currentDate) {
static int serial = 1;
String formattedSerial = String.format("%04d", serial++);
return formattedSerial;
}
}