A
A
Adrenal1ne12016-08-10 10:46:55
Java
Adrenal1ne1, 2016-08-10 10:46:55

Spring+Hibernate+SQL. How to properly handle Unique index or primary key violation exception?

Good afternoon.
The application is written using Spring-4.3.0.RELEASE + Hibernate-4.3.11.Final.
I write to the database (H2DataBase) using Hibernate thousands of rows at a time. The primary key is composite.

@Override
    @Transactional
    public void saveOrUpdate(GeneralStats generalStats) {
        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(generalStats);//исключение получаю здесь
    }

With a non-unique primary key, I get an exception:
ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-6) Violation of a unique index or primary key.
In principle, I also need that non-unique values ​​are filtered out.
How to properly handle this exception? Or before an insertion it is necessary to check for uniqueness a line?
PS If possible, give a small example of handling a spring exception.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question