我开发了一个使用 Hibernate 访问 PostgreSQL 数据库的小应用程序。当我尝试删除与另一个表的外键中涉及的记录时,捕获了一条javax.persistence.PersistenceException
,并显示以下消息:
无法执行语句
但是我注意到在控制台窗口中有一条更详细的消息,如下所示:
13:13:49,222 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) ERROR: update or delete on table "AdminSite" violates foreign key constraint "FK_AdminUserSite" on table "AdminUserProfile"
Detail: Key (STCode)=(HO ) is still referenced from the table "AdminUserProfile".
如何检索此消息更有意义?
Hibernate将与数据库交互时抛出的SQLExceptions包装在JDBCException中。事实上,Hibernate将尝试将异常转换为JDBCException的一个更麻烦的子类。底层 SQLException 始终可通过 JDBCException.getCause(( 获得。
Hibernate文档指定了上述内容,可在Hibernate文档下找到。