我正在使用带有对象“Cliente”的Hibernate(hibernate3.jar),如下所示:
public class Cliente {
private nombre;
...
//set and get
}
“名词”属性映射为:
<property name="nombre" type="string">
<column name="nombre" length="30" not-null="true" />
</property>
正如你在上面看到的,有一个字符长度限制 squals to 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),但控制台中显示了异常,就好像捕获不起作用一样。