Hi everybody!
I'm using Hibernate 4.0 Final and ojdbc6 to develop my web application. Everything is ok except when I try to insert a new parent/child relationship. First of all, these are the entities:
Code:
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable, Cloneable {
@Id
@SequenceGenerator(name = "seq", sequenceName = "P_SEQ")
@GeneratedValue(generator = "seq")
@Column(name = "ID_EMPLOYEE")
private long idEmployee;
......
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "employee", orphanRemoval = true)
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
private Set<Address> addresses;
......
}
@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable, Cloneable, Comparable {
@Id
@SequenceGenerator(name = "seq", sequenceName = "P_SEQ")
@GeneratedValue(generator = "seq")
@Column(name = "ID_ADDRESS")
private long idAddress;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_EMPLOYEE")
private Employee employee;
.......
}
Let's see these two scenes:
1. An employee already exists and I try to add a new address to it --> It works properly.
2. An employee doesn't exist yet and I try to create a new one. Two diferent cases:
- a) I only insert an employee (with no address) --> It works properly.
- b) I insert and employee and an address --> It fails. I have to say that this must be an atomic transaction. I mean, I need to create (save) an employee and one address for it at once. This is the transaction handler:
Code:
public static void save(Employee employee) throws HibernateException, Exception {
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
try {
session.saveOrUpdate(employee);
} catch (Exception ex) {
session.refresh(employee);
HibernateUtil.closeSession();
throw ex;
}
HibernateUtil.commitTransaction();
}
public static void commitTransaction() throws Exception {
Transaction tx = getSessionFactory().getCurrentSession().getTransaction();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
}
} catch (Exception ex) {
tx.rollback();
throw ex;
} finally {
closeSession();
}
}
As you can imagine, the 2.b case is the one I'm concerned about. I've debugged the transaction and this is what I get when I call the save() method (which is in a DAO class):
- The session.saveOrUpdate(employee) method executes successfully. I can check that the employee has an address. Besides its idEmployee is properly set (taken from the sequence). Also the address is binded to the employee and has a valid idAddress (taken from the sequence).
- During the execution of the commitTransaction() method I get an org.hibernate.exception.ConstraintViolationException, although both idEmployee and idAddress are properly set.
In short, the Exception come out just during the commit process. It's like if this began commiting the child (address) instead the parent (employee).
I don't really know what to do. What am I doing wrong? Please, I need your help.