1

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 :

enter image description here

Its not happening continually it will happen any time unable to find reason.

5
  • Are you sure the method is not called twice? Try logging something or using a debugger to be sure. Or are you trying to update a MedicationGroup object, but your primary key is not set so Hibernate thinks it is a new record? Commented Nov 14, 2018 at 8:22
  • please show part of MedicationGroup class where entity annotations and id properties are defined Commented Nov 14, 2018 at 8:23
  • Show how you are calling public MedicationGroup save(MedicationGroup medicationGroup) {}. Which column is your primary key ? Commented Nov 14, 2018 at 8:31
  • @Raheela Aslam I do not think this code alone can be a culprit, might be outside of this you are again saving object or playing with non persisted data. Commented Nov 14, 2018 at 9:21
  • I have added the MedicationGroup class. Actually its uncertain not happening everytime i think there is issue of related to Transaction. Commented Nov 14, 2018 at 10:21

1 Answer 1

1

Are you sure the method is not called twice. Try using primary key in one of the fields, so that duplicate data is not stored in the database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.