I'm trying to persist many records in one transaction using JPA (EclipseLink). The Entry has a primary key defined on DB.
I have some duplicates in my feed so I know there will be a constraint violation. I want to catch a Constraint Violation Exception for a single item and continue persisting others.
Here is My code:
Entity:
@Entity
@Table(name="TEST")
public class TestEntity {
@Id
private String name;
public TestEntity() {
}
public TestEntity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Operation:
@Transactional
public void test() {
List<String> list = Arrays.asList("A", "B","C","D","D","E","F","G");//duplicated D
for(String name: list) {
try {
TestEntity entity = new TestEntity(name);
logger.info("" + entity);
entityManager.persist(entity);
logger.info("Persist done");
entityManager.flush();
logger.info("Flush done");
} catch(RuntimeException e) {
logger.warn("Entry {} was skipped due to following exception {}.", name, e.getLocalizedMessage());
}
}
logger.info("Importing dictionary finished. ");
}
As a result I get constraint violation exception for second D (that's what i expected), but I get this exception for the next items as well and that's the thing I don't understand.
I'm getting: Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (BBHDEV4.SYS_C0023890) violated
What is causing that issue?