How to Prevent Duplicate Results in Hibernate

Prevent duplicate entries in column using hibernate

If I understood what you are asking I think you can annotate the both attributes with:

@Column(unique = true)

It will make your attributes unique, so you will ensure that the data won't be duplicated.

Best way to prevent duplicate rows in database

First, you don't need @Component on your entity.

Second, add this annotation

@Table(name = "table_name", uniqueConstraints={@UniqueConstraint(columnNames ={"id", "departure", ...})})

with fields that should not be duplicated.

NB: you need to add spring.jpa.hibernate.ddl-auto=update to application.properties



Related Topics



Leave a reply



Submit