获取导致SQL Exception对象异常的字段和表名称

时间:2022-09-11 13:41:01

I'm using Hibernate JPA and Spring in my project. Suppose if I try to insert a record with duplicate key value, the insert query will throw an exception.

我在我的项目中使用Hibernate JPA和Spring。假设如果我尝试插入具有重复键值的记录,则插入查询将引发异常。

Now, is it possible to get the table name, field name and possible cause for the exception from the SQL Exception object at run time !?

现在,是否可以在运行时从SQL Exception对象获取表名,字段名和可能的异常原因!

3 个解决方案

#1


As the other answers have mentioned, most of the time you are limited to what you are given by the database. However in some cases Hibernate can tell you the problem - it won't be the table and field, but can be the entity and property

正如其他答案所提到的那样,大多数情况下,您只能使用数据库给出的内容。但是在某些情况下,Hibernate可以告诉你问题 - 它不是表和字段,但可以是实体和属性

For example if you try to put too large a value in a column you can use this:

例如,如果您尝试在列中放入太大的值,则可以使用:

try {
    entityManager.persist(object);

} catch (InvalidStateException e) {

    log.info("Caught Hibernate exception: #0", e.getClass().getName());

    InvalidValue[] invalidValues = e.getInvalidValues();

    for(InvalidValue invalidValue : invalidValues) {
        log.info("Invalid Property #0 has value: #1", invalidValue.getPropertyName(), invalidValue.getPropertyName());
    }
}

#2


SQLExceptions tend to just wrap up database errors, you can only get that sort of information if it was included in the error that your database threw. In my experience of debugging hibernate and JDBC stuff, I would hazard that the answer to this question is generally "no", but there might be exceptions.

SQLExceptions倾向于包装数据库错误,只有在数据库丢失的错误中包含这些信息时才能获得。根据我调试hibernate和JDBC的经验,我担心这个问题的答案通常是“不”,但可能有例外。

#3


hibernate wraps SQLException as JDBCException and tries to convert it to more meaning full exception like ConstraintViolationException using SQLExceptionConverter which is attached to SessionFactory.

hibernate将SQLException包装为JDBCException,并尝试使用附加到SessionFactory的SQLExceptionConverter将其转换为更具意义的完整异常,例如ConstraintViolationException。

SQLException sqlException = ((JDBCException)ex).getSQLException(); 

#1


As the other answers have mentioned, most of the time you are limited to what you are given by the database. However in some cases Hibernate can tell you the problem - it won't be the table and field, but can be the entity and property

正如其他答案所提到的那样,大多数情况下,您只能使用数据库给出的内容。但是在某些情况下,Hibernate可以告诉你问题 - 它不是表和字段,但可以是实体和属性

For example if you try to put too large a value in a column you can use this:

例如,如果您尝试在列中放入太大的值,则可以使用:

try {
    entityManager.persist(object);

} catch (InvalidStateException e) {

    log.info("Caught Hibernate exception: #0", e.getClass().getName());

    InvalidValue[] invalidValues = e.getInvalidValues();

    for(InvalidValue invalidValue : invalidValues) {
        log.info("Invalid Property #0 has value: #1", invalidValue.getPropertyName(), invalidValue.getPropertyName());
    }
}

#2


SQLExceptions tend to just wrap up database errors, you can only get that sort of information if it was included in the error that your database threw. In my experience of debugging hibernate and JDBC stuff, I would hazard that the answer to this question is generally "no", but there might be exceptions.

SQLExceptions倾向于包装数据库错误,只有在数据库丢失的错误中包含这些信息时才能获得。根据我调试hibernate和JDBC的经验,我担心这个问题的答案通常是“不”,但可能有例外。

#3


hibernate wraps SQLException as JDBCException and tries to convert it to more meaning full exception like ConstraintViolationException using SQLExceptionConverter which is attached to SessionFactory.

hibernate将SQLException包装为JDBCException,并尝试使用附加到SessionFactory的SQLExceptionConverter将其转换为更具意义的完整异常,例如ConstraintViolationException。

SQLException sqlException = ((JDBCException)ex).getSQLException();