I have dao layer:
@Transactional
public class DatabaseCollectionDao implements IDatabaseCollectionDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void create(Collection collection) {
entityManager.persist(collection);
}
}
It works correctly but:
- When database isn't available I have SocketException.
- When database contains a duplicate key I have SQLIntegrityConstraintViolationException
I am trying to try/catch it inside create method:
@Override
public void create(Collection collection) {
try{
entityManager.persist(collection);
} catch (SQLIntegrityConstraintViolationException e){
//do smth
}
}
But Intellij says that It is never thrown.
When I try to try/catch Exception i have UnexpectedRollbackException.
How to handle exceptions using JPA entityManager?
update: An attempt to remove @Transactional gave nothing
P.S. To be sure i tried to try/catch it in higher layers. I don't know what i can try more to solve it.
entity.persist(entity);- is this code correct?Entitymanager::persist? docs.oracle.com/javaee/7/api/javax/persistence/…