Hi, i'm trying to save two objects using the save method on the same session, the first object is violating a constraint on database, after it raises an error i rollback the transaction and try to save the second object, when i call the save method for the second object, i'm getting the same error as i was before, its like the rollback didn't work and the commit is trying to save the first object.
ps: I can't call the clear method on the session, cause it would detach some objects from the session
Any ideas?
Thanks.
Code:
Session session = PersistenceUtil.getSession("LOCAL");
Query createQuery = session.createQuery("from Language where id = -1");
List<Language> list = createQuery.list();
Language language = list.get(0);
Transaction beginTransaction = null;
try {
beginTransaction = session.beginTransaction();
//heres the constraint it violates
language.setCode("99");
session.save(language);
session.getTransaction().commit();
} catch (Exception e) {
System.out.println("FIRST ERROR! OK!");
session.getTransaction().rollback();
//session.clear();
}
session.cancelQuery();
createQuery = session.createQuery("from Language where id = -2");
list = createQuery.list();
language = list.get(0);
try {
session.beginTransaction();
language.setName(language.getName() + "_A");
session.save(language);
//i'm getting the same error here again
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
It outputs:
Quote:
org.hibernate.exception.ConstraintViolationException: could not update: [br.com.dyad.infrastructure.entity.Language#-1]