-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Integrity constraint violated just when I commit transaction
PostPosted: Fri Mar 08, 2013 5:07 am 
Newbie

Joined: Fri Mar 08, 2013 2:56 am
Posts: 2
Location: Sevilla, Spain
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.