Spring @Repository异常翻译

时间:2018-11-13 08:10:53

标签: java spring exception-handling spring-data-jpa

我试图弄清楚@Repository批注提供了哪些好处。我知道@Component,@ Service,@ Repository基本上是构造型注释,主要用于分隔层和应用AOP切入点。我唯一发现@repository提供自动异常翻译。我的问题是? 我使用JDBC模板和H2数据库实现了以下dao类:

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public void saveUser(Users user) {
        try {
            jdbcTemplate.update("insert into users(name,address,age) values (?,?,?)",
                    new Object[] { user.getName(), user.getAddress(), null });
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

}

实体类为:

@Entity
public class Users {

    @Column(name = "name", nullable = true)
    private String name;

    @Column(name = "address", nullable = true)
    private String address;

    @Column(name = "age", nullable = false)
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

数据库中的“年龄”列没有非空约束。如果使用@Repository和使用@component或@service的普通H2 DB异常,我期望将异常作为DataAccessException包装的对象。但是它没有发生。无论我使用什么注释,它都显示相同的排他轨迹。

org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [insert into users(name,address,age) values (?,?,?)]; integrity constraint violation: NOT NULL check constraint; SYS_CT_10092 table: USERS column: AGE; nested exception is java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10092 table: USERS column: AGE
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:87)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1414)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:917)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
    at com.spring.practice.dao.daoimpl.UserDaoImpl.saveUser(UserDaoImpl.java:18)
    at com.spring.practice.service.serviceimpl.UserServiceImpl.saveUser(UserServiceImpl.java:38)
    at com.spring.practice.main.MainClass.main(MainClass.java:14)
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10092 table: USERS column: AGE
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    at org.springframework.jdbc.core.JdbcTemplate.lambda$update$0(JdbcTemplate.java:867)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
    ... 6 more
Caused by: org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10092 table: USERS column: AGE

所以我唯一的困惑是@repository有什么好处?

谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了答案。在这两个原因中,我都得到了相同的异常跟踪,因为我正在使用JDBCTemplate。 JDBCTemplate在内部处理此任务,因此无论如何都将获得相同的异常跟踪。我使用了hibernate而不是JDBC模板,并且异常跟踪符合预期。 PFB跟踪:

带有组件注释:

Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at com.spring.practice.dao.daoimpl.UserDaoImpl.saveUser(UserDaoImpl.java:25)
    at com.spring.practice.service.serviceimpl.UserServiceImpl.saveUser(UserServiceImpl.java:38)
    at com.spring.practice.main.MainClass.main(MainClass.java:14)

带有存储库注释(将异常转换为Spring异常):

Exception in thread "main" org.springframework.orm.hibernate4.HibernateSystemException: No Session found for current thread; nested exception is org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:216)
    at org.springframework.orm.hibernate4.HibernateExceptionTranslator.convertHibernateAccessException(HibernateExceptionTranslator.java:57)
    at org.springframework.orm.hibernate4.HibernateExceptionTranslator.translateExceptionIfPossible(HibernateExceptionTranslator.java:44)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy16.saveUser(Unknown Source)
    at com.spring.practice.service.serviceimpl.UserServiceImpl.saveUser(UserServiceImpl.java:38)
    at com.spring.practice.main.MainClass.main(MainClass.java:14)
Caused by: org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at com.spring.practice.dao.daoimpl.UserDaoImpl.saveUser(UserDaoImpl.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 5 more

在jdbcTemplate内部由jdbcTemplate处理的情况下,这种翻译将永远不会发生。因此,在使用jdbcTemplate的情况下,我们不需要PersistenceExceptionTranslationPostProcessor。