Skip to content

Commit f8162fe

Browse files
committed
Adjust MyBatisSystemException to Spring 6
Since Spring 6, `org.springframework.core.NestedRuntimeException` no longer includes the message of the wrapped exception. spring-projects/spring-framework#25162 Should fix mybatisgh-887
1 parent 8d8e362 commit f8162fe

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,8 +82,12 @@ public DataAccessException translateExceptionIfPossible(RuntimeException e) {
8282
if (e instanceof PersistenceException) {
8383
// Batch exceptions come inside another PersistenceException
8484
// recursion has a risk of infinite loop so better make another if
85+
String msg = e.getMessage();
8586
if (e.getCause() instanceof PersistenceException) {
8687
e = (PersistenceException) e.getCause();
88+
if (msg == null) {
89+
msg = e.getMessage();
90+
}
8791
}
8892
if (e.getCause() instanceof SQLException) {
8993
this.initExceptionTranslator();
@@ -94,7 +98,7 @@ public DataAccessException translateExceptionIfPossible(RuntimeException e) {
9498
} else if (e.getCause() instanceof TransactionException) {
9599
throw (TransactionException) e.getCause();
96100
}
97-
return new MyBatisSystemException(e);
101+
return new MyBatisSystemException(msg, e);
98102
}
99103
return null;
100104
}

src/main/java/org/mybatis/spring/MyBatisSystemException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,8 +31,8 @@ public class MyBatisSystemException extends UncategorizedDataAccessException {
3131

3232
private static final long serialVersionUID = -5284728621670758939L;
3333

34-
public MyBatisSystemException(Throwable cause) {
35-
super(null, cause);
34+
public MyBatisSystemException(String msg, Throwable cause) {
35+
super(msg, cause);
3636
}
3737

3838
}

0 commit comments

Comments
 (0)