I am new in Spring Data, and I need to establish the impossibility of creating a new entity in DB if an entity already exists with the same field values.
Comparison condition: if "closeType" field and "id" agreement field of a new entity equal to database entity fields, I can't add this entity to DB. How do it?
My entity:
@Entity
@Table(name = "contract")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Contract implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contractGenerator")
@SequenceGenerator(name = "contractGenerator", sequenceName = "contract_sequence")
private Long id;
@Column(name = "start_date")
private LocalDate startDate;
@Column(name = "end_date")
private LocalDate endDate;
@Column(name = "first_pay_date")
private LocalDate firstPayDate;
@Column(name = "next_pay_date")
private LocalDate nextPayDate;
//Here is thу first field for comparison
@Enumerated(EnumType.STRING)
@Column(name = "close_type")
private CloseType closeType;
@ManyToOne
@JsonIgnoreProperties("")
private Mentor mentor;
//Here is second ID agreement field for comparison
@ManyToOne
@JsonIgnoreProperties("")
private Agreement agreement;
...............
//getters and setters
I have to block possibility to create several active contracts("closeType") in one agreement ("id")