gpt4 book ai didi

java - org.hibernate.exception.DataException 虽然被捕获

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:41 25 4
gpt4 key购买 nike

我正在使用 Hibernate (hibernate3.jar) 和对象“Cliente”,如下所示:

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

“nombre”属性映射为:

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

正如您在上面看到的,字符长度限制为 30。这里事情变得复杂......我试图用长名称更新名称以强制发生错误:

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

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),但异常显示在控制台中,就好像捕获不起作用一样。

最佳答案

给出列名 nombre 的大长度。例如

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

或者删除长度属性。它将根据您的数据库供应商自动为字符串定义最大值

请参阅下面的链接

Solution1

另请参阅下面的链接

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

关于java - org.hibernate.exception.DataException 虽然被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com