Olá! Gostaria de saber como fazer um POST que tenha um campo UNIQUE no BD e tratar o erro. Fiz a alteração seguinte na classe Topico:
@Column(unique = true)
private String titulo;
E deu o erro abaixo:
{
"timestamp": "2021-01-20T16:22:59.811+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not execute statement; SQL [n/a]; constraint [\"PUBLIC.UK_F1FX5U7UJ9MA3W8FLKN3H683M_INDEX_9 ON PUBLIC.TOPICO(TITULO) VALUES 1\"; SQL statement:\ninsert into topico (id, autor_id, curso_id, data_criacao, mensagem, status, titulo) values (null, ?, ?, ?, ?, ?, ?) [23505-199]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"trace": "org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [\"PUBLIC.UK_F1FX5U7UJ9MA3W8FLKN3H683M_INDEX_9 ON PUBLIC.TOPICO(TITULO) VALUES 1\"; SQL statement:\ninsert into topico (id, autor_id, curso_id, data_criacao, mensagem, status, titulo) values (null, ?, ?, ?, ?, ?, ?) [23505-199]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:296)\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)\r\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)\r\n\tat org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)\r\n\tat org.springframework.dao.support.DataAccessUtils.translateIfNecessary