org.hibernate.exception.DataException虽然被捕获 [英] org.hibernate.exception.DataException although is catched

查看:421
本文介绍了org.hibernate.exception.DataException虽然被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate(hibernate3.jar)和对象 Cliente,就像这样:

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

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

nombre属性映射为:

The "nombre" attribute is mapped as:

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

如上所示,字符长度限制等于 30 。这里的事情变得复杂起来...我试图用长名称更新名称以强制执行错误:

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();
}

名称超过允许的限制时,此例外抛出org.hibernate.exception.DataException (作为调试器的详细信息,它位于 x.commit();

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

这里出了什么问题?捕获(显示JOPtion),则在控制台中显示异常,好像捕获不起作用。

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.

推荐答案

列名nombre的大长度。例如

Give the large length for column name nombre. For example

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

或删除length属性。它将根据您的数据库供应商自动为String定义最大值。

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

请参见以下链接

解决方案1 ​​

另请参见以下链接

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

这篇关于org.hibernate.exception.DataException虽然被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆