I have a spring boot application with MySQL database. Below method is creating medication's two rows with same fields.
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public MedicationGroup save(MedicationGroup medicationGroup) {
return medicationRepository.save(medicationGroup);
}
Medication Group Entity:
@Getter
@Setter
@Table(name = "medication_group")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MedicationGroup extends AbstractEntity implements Persistable {
private static final long serialVersionUID = 2948809916398284974L;
private Short type;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "patient_id", nullable = false, updatable = false, insertable = false)
private Patient patient;
@Column(name = "patient_id")
private Long patientId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "medicationGroup", cascade = CascadeType.ALL)
private List<Prescription> prescriptions;
}
Below rows are created into database :
Its not happening continually it will happen any time unable to find reason.

MedicationGroupobject, but your primary key is not set so Hibernate thinks it is a new record?MedicationGroupclass where entity annotations and id properties are definedpublic MedicationGroup save(MedicationGroup medicationGroup) {}. Which column is your primary key ?