I have a table called Topic with two columns: Id(Primary key) and topic(Unique key).
I need to avoid duplicate entries to column 'topic'.
Here is the domain class for the table.
@Entity @Table(name="TOPIC",uniqueConstraints = {@UniqueConstraint(columnNames = "TOPIC") }) @DynamicUpdate ublic class Topic { @Id @GeneratedValue @Column(name="ID") private long id; @Column(name="TOPIC",unique=true) private String topic; //getters and setters When I'am inserting duplicate entry,it is trowing sql exception (org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Duplicate entry ).So iam catching that exception and showing a message "item already exists)
try { topicService.save(tp); } catch (Exception e) { System.out.print("item already exists"); } is this the correct method for dealing with unique constraints in Spring data JPA?