I am trying to figure out what benefits does the @Repository annotation provides. I understand that @Component ,@Service ,@Repository are basically stereotype annotation that are mainly used for segregating the layers and applying the AOP pointcuts. The only thing I found out that @repository provides automatic exception translation. My question is how? I implemented the below dao class using JDBC template and H2 Database :
@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();
}
}
}
The entity class is :
@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;
}
}
There is a not null constraint on "age" column in DB. I was expecting the exception as DataAccessException wrapped object in case I use @Repository and normal H2 DB exception if I use @component or @service. But its not happening. It's showing the same excpetion trace no matter what annotation I use.
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
So my only confusion is that what benefits does @repository provide?
Thanks in advance.