Это моя пружинная конфигурация: < Br />
Код: Выделить всё
@Configuration
@ComponentScan
public class AppConfig {
@Bean
DataSource getDataSource() {
String dbUrl = mySqlUrl;
String dbUser = "user";
String dbPassword = "password";
MysqlDataSource mysqlDS = null;
mysqlDS = new MysqlDataSource();
mysqlDS.setURL(dbUrl);
mysqlDS.setUser(dbUser);
mysqlDS.setPassword(dbPassword);
return mysqlDS;
}
@Bean
static public PersistenceExceptionTranslationPostProcessor translation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Код: Выделить всё
@Repository
public class UsersDAO {
DataSource dataSource;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
void insertUser(User user) {
try(Connection conn = this.dataSource.getConnection()) {
try(PreparedStatement pstmt = conn.prepareStatement("insert into users (id, name) values (?, ?)")) {
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getName());
int result = pstmt.executeUpdate();
}
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
}
< /code>
edit < /h3>
Я также попробовал без ущерба и переосмысления: < /p>
void insertUser(User user) throws SQLException {
try(Connection conn = this.dataSource.getConnection()) {
try(PreparedStatement pstmt = conn.prepareStatement("insert into users (id, name) values (?, ?)")) {
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getName());
int result = pstmt.executeUpdate();
}
}
< /code>
и приложение: < /p>
public class App {
public static void main(String[] args) throws SQLException {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UsersDAO dao = (UsersDAO)context.getBean("usersDAO");
User john = new User(5, "John");
dao.insertUser(john);
}
}
Код: Выделить всё
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '5' for key 'users.PRIMARY'
at org.example.UsersDAO.insertUser(UsersDAO.java:58)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:354)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:720)
at org.example.UsersDAO$$SpringCGLIB$$0.insertUser()
at org.example.App.main(App.java:27)
Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '5' for key 'users.PRIMARY'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1320)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:994)
at org.example.UsersDAO.insertUser(UsersDAO.java:54)
... 14 more
Кто -нибудь знает, почему Spring не переводит SQLEXCEPTION (или подтип в этом случае) в DataCceScessException (или подтип).
Подробнее здесь: https://stackoverflow.com/questions/793 ... sexception
Мобильная версия