-->
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.  [ 2 posts ] 
Author Message
 Post subject: remove obj from session after MySQLIntegrityConstraintVion
PostPosted: Thu Oct 14, 2010 10:18 am 
Newbie

Joined: Thu Oct 14, 2010 9:23 am
Posts: 3
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;
   }
}


Last edited by paulbeuk on Thu Oct 14, 2010 10:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: remove obj from session after MySQLIntegrityConstraintVion
PostPosted: Thu Oct 14, 2010 10:29 am 
Newbie

Joined: Thu Oct 14, 2010 9:23 am
Posts: 3
Ha,

Just posting the issue gave me the answer.
session.clear() seems to solve this issue.

Code:
    private void save( Test test , Session session ) {
       try {
          session.save(test);
       } catch ( org.hibernate.exception.ConstraintViolationException cve ) {
          System.err.println("error: " + cve.getCause() );
          session.clear();
       }
    }


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

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.