Hi there,
Hope you can help me with the following issue.
I want to store a number of objects, but after saving one of the objects the session throws a org.hibernate.exception.ConstraintViolationException.
No problem, but now I can NOT commit the transaction to store the other valid objects. Can anybody tell me how?
How can I remove the invalid object from the transaction?
TIA,
Paul van Beukering
Manager: src/main/java/org/hibernate/tutorial/web/TestManager.java
Code:
package org.hibernate.tutorial.web;
import org.hibernate.Session;
import org.hibernate.tutorial.domain.Test;
import org.hibernate.tutorial.util.HibernateUtil;
public class TestManager {
public static void main(String[] args) {
TestManager mgr = new TestManager();
mgr.doTest();
HibernateUtil.getSessionFactory().close();
}
private void doTest() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Test test = new Test("john");
save(test,session);
Test test2 = new Test("john");
save(test2,session);
session.getTransaction().commit();
}
private void save( Test test , Session session ) {
try {
session.save(test);
} catch ( org.hibernate.exception.ConstraintViolationException cve ) {
System.err.println("error: " + cve.getCause() );
}
}
}
object: src/main/java/org/hibernate/tutorial/domain/Test.java
Code:
package org.hibernate.tutorial.domain;
public class Test {
private String username;
private Long id;
public Test() {
}
public Test( String username ) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername( String username ) {
this.username = username;
}
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}
}