1

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.

manix
  • 14,537
  • 11
  • 70
  • 107

2 Answers2

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
Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Yes, but when attribute is removed, the exception persists. Supposing that I am using a column `nombre varchar(255)` in mysql db. If I set a name with a large size than this, the exception is thrown too. – manix Apr 13 '13 at 02:09
  • Go to DB client and see what is the length of column Nombre. It should be of length 255 – M Sach Apr 13 '13 at 02:22
  • Yes, it is set with 255. Let me try your link. – manix Apr 13 '13 at 02:34
0

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.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69