-->
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: Catch ConstraintViolationException
PostPosted: Sun May 06, 2007 5:34 pm 
Newbie

Joined: Sun May 06, 2007 5:57 am
Posts: 10
I'm using a unique key on one column of my DB table and I'll get a ConstraintViolationException as expected. Now I want to try to catch this Exception, but it doesn't work.
Can anyone help me? I made a small simplified example:

Mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 06.05.2007 23:22:55 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
    <class name="de.test.Test" table="test" catalog="test">
        <comment></comment>
        <id name="idTest" type="int">
            <column name="idTest" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.Integer">
            <column name="name" unique="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>


POJO:
Code:
package de.test;

// Generated 06.05.2007 23:22:55 by Hibernate Tools 3.2.0.b9

/**
* Test generated by hbm2java
*/
public class Test implements java.io.Serializable {

   private int idTest;

   private Integer name;

   public Test() {
   }

   public Test(int idTest) {
      this.idTest = idTest;
   }

   public Test(int idTest, Integer name) {
      this.idTest = idTest;
      this.name = name;
   }

   public int getIdTest() {
      return this.idTest;
   }

   public void setIdTest(int idTest) {
      this.idTest = idTest;
   }

   public Integer getName() {
      return this.name;
   }

   public void setName(Integer name) {
      this.name = name;
   }

}


Class for testing:
Code:
package de.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.exception.ConstraintViolationException;

public class Main {
   
   public static void main(String ... args) {
      SessionFactory sessionFactory;
      try{
         sessionFactory = new Configuration()
                     .configure("hibernate.cfg.xml")
                     .buildSessionFactory();
      }
      catch(Throwable ex){
         throw new ExceptionInInitializerError(ex);         
      }
      
      Session session = sessionFactory.getCurrentSession();
      session.beginTransaction();
      
      Test test = new Test(1,2);
      Test test2 = new Test(2,2);
      try {
         session.saveOrUpdate(test);
         session.saveOrUpdate(test2);   
      } catch (ConstraintViolationException cve) {
         cve.printStackTrace();
      }
      
      System.out.println("Never comes: Script go on!");

      session.getTransaction().commit();
   }
}


And now the Exception:
Quote:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at de.test.Main.main(Main.java:35)
Caused by: java.sql.BatchUpdateException: Duplicate entry '2' for key 'Unique_Index'
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 8 more


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 5:44 pm 
Newbie

Joined: Sun May 06, 2007 5:57 am
Posts: 10
Sorry for post. Problem solved:

Code:
package de.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.exception.ConstraintViolationException;

public class Main {
   
   public static void main(String ... args) {
      SessionFactory sessionFactory;
      try{
         sessionFactory = new Configuration()
                     .configure("hibernate.cfg.xml")
                     .buildSessionFactory();
      }
      catch(Throwable ex){
         throw new ExceptionInInitializerError(ex);         
      }
     
      Session session = sessionFactory.getCurrentSession();
      session.beginTransaction();
     
      Test test = new Test(1,2);
      Test test2 = new Test(2,2);
      try {
         session.saveOrUpdate(test);
         session.saveOrUpdate(test2);   
         session.getTransaction().commit();
      } catch (ConstraintViolationException cve) {
         cve.printStackTrace();
      }
     
      System.out.println("Never comes: Script go on!");


   }
}


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.