org.hibernate.exception.DataException although is catched

别等时光非礼了梦想. 提交于 2019-12-24 11:25:18

问题


I am working with Hibernate (hibernate3.jar) with and object "Cliente", like this:

public class Cliente {
    private nombre;
    ...
    //set and get
}

The "nombre" attribute is mapped as:

<property name="nombre" type="string">
    <column name="nombre" length="30" not-null="true" />
</property>

As you can see above, there is a character length limit squals to 30. Here the things get complicated... I am trying to update the name with a long name in order to force an error:

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

try{
    cliente.setNombre(textField.getText()); //here
    session.update(cliente);
    tx.commit();
    list.repaint();
} catch (org.hibernate.exception.DataException e) {
    JOptionPane.showMessageDialog(null, "Data entered too long");
    session.getTransaction().rollback();
} finally {
    session.close();
}

When the name exceeds the limit allowed, this excepcion org.hibernate.exception.DataException is thrown (as debbuger details, it is at line x.commit();:

SEVERE: Data truncation: Data too long for column 'nombre' at row 1
Hibernate: update gimnasiobd.cliente set nombre=? where idCliente=?
abr 12, 2013 7:40:07 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update

What's the problem here? Well... although the excepcion is catched (the JOPtion is shown), the exception is shown in the console as if the catch does not work.


回答1:


Give the large length for column name nombre. For example

<column name="nombre" length="200" not-null="true" />

Or remove length attribute . It will take max value automatically defined for String as per your DB vendor

see below links

Solution1

also see below link

https://stackoverflow.com/questions/1281188/text-field-using-hibernate-annotation



回答2:


Surround the rollback() with another try-catch to see if it's the cause of the exception

catch (org.hibernate.exception.DataException e) {
    JOptionPane.showMessageDialog(null, "Data entered too long");
    try {
        session.getTransaction().rollback();
    } catch (HibernateException ex) {}
}

I wasn't able to determine from the documentation why a rollback would be throwing a DataException, but the javadoc states that the rollback can throw a HibernateException which is a superclass of DataException.



来源:https://stackoverflow.com/questions/15983371/org-hibernate-exception-dataexception-although-is-catched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!