java组织。冬眠例外虽然捕获到了DataException
我正在使用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
(作为debbuger详细信息,它位于第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),但控制台中会显示异常,就好像捕获不起作用一样
# 1 楼答案
用另一个try catch环绕
rollback()
,看看它是否是异常的原因我无法从文档中确定为什么回滚会引发DataException,但javadoc声明回滚可以引发HibernateeException,这是DataException的超类
# 2 楼答案
为列名nombre指定较大的长度。比如
或者删除长度属性。它将采用根据DB供应商自动为字符串定义的最大值
请参阅以下链接
Solution1
另请参见下面的链接