Преглед на файлове

jpa dao exception 处理

smallflyelephant666 преди 7 години
родител
ревизия
fbfb5bdc5a

+ 8
- 0
src/main/java/ltass/staudy/spring/data/jpa/constant/Constants.java Целия файл

@@ -0,0 +1,8 @@
1
+package ltass.staudy.spring.data.jpa.constant;
2
+
3
+public class Constants {
4
+
5
+    public final static Integer DEL_FLAG_FALSE    = 0;
6
+    public final static Integer DEL_FLAG_TRUE     = 1;
7
+
8
+}

+ 40
- 30
src/main/java/ltass/staudy/spring/data/jpa/controller/TestController.java Целия файл

@@ -3,6 +3,7 @@ package ltass.staudy.spring.data.jpa.controller;
3 3
 import com.alibaba.fastjson.JSON;
4 4
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
5 5
 import ltass.staudy.spring.data.jpa.entity.User;
6
+import ltass.staudy.spring.data.jpa.exception.JpaDaoException;
6 7
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
7 8
 import org.springframework.beans.factory.annotation.Autowired;
8 9
 import org.springframework.web.bind.annotation.GetMapping;
@@ -35,7 +36,7 @@ public class TestController {
35 36
         try{
36 37
             userJpaDao.insert(user);
37 38
         }catch (Exception ex){
38
-            return dealException(ex);
39
+            return JSON.toJSONString(ex);
39 40
         }
40 41
         return "OK.";
41 42
     }
@@ -49,7 +50,7 @@ public class TestController {
49 50
         try{
50 51
             userJpaDao.insert(user);
51 52
         }catch (Exception ex){
52
-            return dealException(ex);
53
+            return JSON.toJSONString(ex);
53 54
         }
54 55
         return "OK.";
55 56
     }
@@ -71,8 +72,12 @@ public class TestController {
71 72
         user.setUpdateProcess("web");
72 73
         try{
73 74
             userJpaDao.insert(user);
75
+        }catch (JpaDaoException ex){
76
+            return "jpadaoException";
77
+            //return JSON.toJSONString(ex);
74 78
         }catch (Exception ex){
75
-            return dealException(ex);
79
+            return "exception";
80
+            //return JSON.toJSONString(ex);
76 81
         }
77 82
         return "OK.";
78 83
     }
@@ -134,7 +139,7 @@ public class TestController {
134 139
         try{
135 140
             userJpaDao.insert(user);
136 141
         }catch (Exception ex){
137
-            return dealException(ex);
142
+            return JSON.toJSONString(ex);
138 143
         }
139 144
         return "OK.";
140 145
     }
@@ -158,7 +163,7 @@ public class TestController {
158 163
         try{
159 164
             userJpaDao.insert(user);
160 165
         }catch (Exception ex){
161
-            return dealException(ex);
166
+            return JSON.toJSONString(ex);
162 167
         }
163 168
         return "OK.";
164 169
     }
@@ -183,7 +188,7 @@ public class TestController {
183 188
         try{
184 189
             userJpaDao.update(user);
185 190
         }catch (Exception ex){
186
-            return dealException(ex);
191
+            return JSON.toJSONString(ex);
187 192
         }
188 193
         return "OK.";
189 194
     }
@@ -208,7 +213,7 @@ public class TestController {
208 213
         try{
209 214
             userJpaDao.update(user);
210 215
         }catch (Exception ex){
211
-            return dealException(ex);
216
+            return JSON.toJSONString(ex);
212 217
         }
213 218
         return "OK.";
214 219
     }
@@ -233,7 +238,7 @@ public class TestController {
233 238
         try{
234 239
             userJpaDao.update(user);
235 240
         }catch (Exception ex){
236
-            return dealException(ex);
241
+            return JSON.toJSONString(ex);
237 242
         }
238 243
         return "OK.";
239 244
     }
@@ -257,7 +262,7 @@ public class TestController {
257 262
         try{
258 263
             userJpaDao.update(user);
259 264
         }catch (Exception ex){
260
-            return dealException(ex);
265
+            return JSON.toJSONString(ex);
261 266
         }
262 267
         return "OK.";
263 268
     }
@@ -280,7 +285,30 @@ public class TestController {
280 285
         try{
281 286
             userJpaDao.update(user);
282 287
         }catch (Exception ex){
283
-            return dealException(ex);
288
+            return JSON.toJSONString(ex);
289
+        }
290
+        return "OK.";
291
+    }
292
+
293
+    //{"causeBy":"org.hibernate.exception.ConstraintViolationException","message":"could not execute statement"}
294
+    @GetMapping("/user/update/error/version_null")
295
+    public String updateErrorVersionNull() {
296
+        User user = new User();
297
+        user.setId(1L);
298
+        user.setNickName("12345");
299
+        user.setName("myName");
300
+        user.setEmail("email");
301
+        user.setRegisterDatetime("2019/01/01 01:01:00");
302
+        user.setRegisterUserId(1L);
303
+        user.setRegisterProcess("web");
304
+        user.setUpdateDatetime("2019/01/01 01:01:00");
305
+        user.setUpdateUserId(1L);
306
+        user.setUpdateProcess("web");
307
+
308
+        try{
309
+            userJpaDao.update(user);
310
+        }catch (Exception ex){
311
+            return JSON.toJSONString(ex);
284 312
         }
285 313
         return "OK.";
286 314
     }
@@ -291,7 +319,7 @@ public class TestController {
291 319
         try{
292 320
             userJpaDao.deleteById(8L);
293 321
         }catch (Exception ex){
294
-            return dealException(ex);
322
+            return JSON.toJSONString(ex);
295 323
         }
296 324
         return "OK.";
297 325
     }
@@ -302,27 +330,9 @@ public class TestController {
302 330
         try{
303 331
             userJpaDao.deleteById(1L);
304 332
         }catch (Exception ex){
305
-            return dealException(ex);
333
+            return JSON.toJSONString(ex);
306 334
         }
307 335
         return "OK.";
308 336
     }
309 337
 
310
-    private String dealException(Exception ex){
311
-        Map<String,Map<String,Object>> retMap = new LinkedHashMap<>();
312
-
313
-        Map<String,Object> retValue1 = new LinkedHashMap<>();
314
-        retValue1.put("exClass",ex.getClass());
315
-        retValue1.put("message",ex.getMessage());
316
-
317
-        retMap.put("ex",retValue1);
318
-
319
-        Map<String,Object> retValue2 = new LinkedHashMap<>();
320
-        retValue2.put("causeBy",ex.getCause().getClass());
321
-        retValue2.put("message",ex.getCause().getMessage());
322
-
323
-        retMap.put("exCause",retValue2);
324
-
325
-        return JSON.toJSONString(retMap);
326
-    }
327
-
328 338
 }

+ 42
- 3
src/main/java/ltass/staudy/spring/data/jpa/dao/impl/UserJpaDaoImpl.java Целия файл

@@ -1,24 +1,63 @@
1 1
 package ltass.staudy.spring.data.jpa.dao.impl;
2 2
 
3
+import cn.hutool.core.date.DateUtil;
4
+import ltass.staudy.spring.data.jpa.constant.Constants;
3 5
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
4 6
 import ltass.staudy.spring.data.jpa.entity.User;
7
+import ltass.staudy.spring.data.jpa.exception.ExceptionHelper;
8
+import ltass.staudy.spring.data.jpa.exception.JpaDaoNoPrimaryException;
9
+import ltass.staudy.spring.data.jpa.exception.JpaDaoNoVerionInfoException;
5 10
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
11
+import ltass.staudy.spring.data.jpa.utils.IdUtils;
6 12
 import org.springframework.beans.factory.annotation.Autowired;
7 13
 import org.springframework.stereotype.Service;
8 14
 
9 15
 @Service
10
-public class UserJpaDaoImpl implements UserJpaDao {
16
+public class UserJpaDaoImpl implements UserJpaDao{
11 17
     @Autowired
12 18
     UserRepository repository;
13 19
 
14 20
     @Override
15 21
     public User insert(User entity) {
16
-        return repository.save(entity);
22
+        if(entity.getId()==null)entity.setId(IdUtils.id());
23
+
24
+        entity.setDelFlag(Constants.DEL_FLAG_FALSE);
25
+
26
+        String now = DateUtil.now();
27
+        entity.setRegisterDatetime(now);
28
+        entity.setUpdateDatetime(now);
29
+
30
+        if(entity.getRegisterProcess()==null){
31
+            entity.setRegisterProcess("web");
32
+            entity.setUpdateProcess("web");
33
+        }
34
+
35
+        try{
36
+            repository.save(entity);
37
+        }catch (Exception e){
38
+            ExceptionHelper.insertExceptionProcess(e,"user","insert");
39
+        }
40
+
41
+        return entity;
17 42
     }
18 43
 
19 44
     @Override
20 45
     public User update(User entity) {
21
-        return repository.save(entity);
46
+        if(entity.getId()==null) throw new JpaDaoNoPrimaryException("user","update");
47
+        if(entity.getVerNo()==null) throw new JpaDaoNoVerionInfoException("user","update");
48
+
49
+        String now = DateUtil.now();
50
+        entity.setUpdateDatetime(now);
51
+
52
+        if(entity.getUpdateProcess()==null)entity.setUpdateProcess("web");
53
+
54
+       try{
55
+            repository.save(entity);
56
+        }catch (Exception e){
57
+           ExceptionHelper.updateExceptionProcess(e,"user","update");
58
+        }
59
+
60
+       return entity;
22 61
     }
23 62
 
24 63
     @Override

+ 68
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/ExceptionHelper.java Целия файл

@@ -0,0 +1,68 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+import org.hibernate.exception.DataException;
4
+import org.springframework.dao.DataIntegrityViolationException;
5
+import org.springframework.orm.ObjectOptimisticLockingFailureException;
6
+
7
+public class ExceptionHelper {
8
+
9
+    public static void insertExceptionProcess(Exception e,String table,String action){
10
+
11
+        StringBuilder sb = new StringBuilder();
12
+        sb.append("class:").append(e.getClass().getName());
13
+        sb.append("message:").append(e.getMessage());
14
+        sb.append("causeBy:").append(e.getCause().getClass().getName());
15
+        sb.append("causeByMessage:").append(e.getCause().getMessage());
16
+
17
+        if(e instanceof DataIntegrityViolationException){
18
+            if(e.getCause() instanceof DataException) {
19
+                throw new JpaDaoDataException(sb.toString(), table, action);
20
+            }else{
21
+                String constraintField = getConstraintField(e.getMessage());
22
+                if("PRIMARY".equalsIgnoreCase(constraintField)){
23
+                    throw new JpaDaoConflictIdException(sb.toString(),table,action);
24
+                }if("null".equalsIgnoreCase(constraintField)){
25
+                    throw new JpaDaoConflictNullException(sb.toString(),table,action);
26
+                }else{
27
+                    throw new JpaDaoConflictUniqueException(sb.toString(),table,action);
28
+                }
29
+            }
30
+        }
31
+        throw new JpaDaoException(sb.toString(),table,action);
32
+    }
33
+
34
+    public static void updateExceptionProcess(Exception e,String table,String action){
35
+
36
+        StringBuilder sb = new StringBuilder();
37
+        sb.append("class:").append(e.getClass().getName());
38
+        sb.append("message:").append(e.getMessage());
39
+        sb.append("causeBy:").append(e.getCause().getClass().getName());
40
+        sb.append("causeByMessage:").append(e.getCause().getMessage());
41
+
42
+        if(e instanceof DataIntegrityViolationException){
43
+            if(e.getCause() instanceof DataException) {
44
+                throw new JpaDaoDataException(sb.toString(), table, action);
45
+            }else{
46
+                String constraintField = getConstraintField(e.getMessage());
47
+                if("PRIMARY".equalsIgnoreCase(constraintField)){
48
+                    throw new JpaDaoConflictIdException(sb.toString(),table,action);
49
+                }if("null".equalsIgnoreCase(constraintField)){
50
+                    throw new JpaDaoConflictNullException(sb.toString(),table,action);
51
+                }else{
52
+                    throw new JpaDaoConflictUniqueException(sb.toString(),table,action);
53
+                }
54
+            }
55
+        }else if(e instanceof ObjectOptimisticLockingFailureException){
56
+            throw new JpaDaoOptimisticLockingFailureException(sb.toString(),table,action);
57
+        }
58
+        throw new JpaDaoException(sb.toString(),table,action);
59
+    }
60
+
61
+    private static String getConstraintField(String str){
62
+        int c_start = str.indexOf("constraint");
63
+        int start = str.indexOf("[",c_start);
64
+        int end = str.indexOf("]",c_start);
65
+
66
+        return str.substring(start+1,end);
67
+    }
68
+}

+ 15
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoConflictIdException.java Целия файл

@@ -0,0 +1,15 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoConflictIdException extends RuntimeException{
4
+    private String rawMessage;
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoConflictIdException(String rawMessage, String table, String action) {
9
+        super();
10
+        this.rawMessage = rawMessage;
11
+        this.table = table;
12
+        this.action = action;
13
+    }
14
+
15
+}

+ 15
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoConflictNullException.java Целия файл

@@ -0,0 +1,15 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoConflictNullException extends RuntimeException{
4
+    private String rawMessage;
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoConflictNullException(String rawMessage, String table, String action) {
9
+        super();
10
+        this.rawMessage = rawMessage;
11
+        this.table = table;
12
+        this.action = action;
13
+    }
14
+
15
+}

+ 15
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoConflictUniqueException.java Целия файл

@@ -0,0 +1,15 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoConflictUniqueException extends RuntimeException{
4
+    private String rawMessage;
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoConflictUniqueException(String rawMessage, String table, String action) {
9
+        super();
10
+        this.rawMessage = rawMessage;
11
+        this.table = table;
12
+        this.action = action;
13
+    }
14
+
15
+}

+ 19
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoDataException.java Целия файл

@@ -0,0 +1,19 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+import lombok.Getter;
4
+
5
+@Getter
6
+public class JpaDaoDataException extends RuntimeException {
7
+
8
+    private String rawMessage;
9
+    private String table;
10
+    private String action;
11
+
12
+    public JpaDaoDataException(String rawMessage, String table, String action) {
13
+        super();
14
+        this.rawMessage = rawMessage;
15
+        this.table = table;
16
+        this.action = action;
17
+    }
18
+
19
+}

+ 19
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoException.java Целия файл

@@ -0,0 +1,19 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+import lombok.Getter;
4
+
5
+@Getter
6
+public class JpaDaoException extends RuntimeException {
7
+
8
+    private String rawMessage;
9
+    private String table;
10
+    private String action;
11
+
12
+    public JpaDaoException(String rawMessage,String table,String action) {
13
+        super();
14
+        this.rawMessage = rawMessage;
15
+        this.table = table;
16
+        this.action = action;
17
+    }
18
+
19
+}

+ 14
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoNoPrimaryException.java Целия файл

@@ -0,0 +1,14 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoNoPrimaryException extends RuntimeException{
4
+
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoNoPrimaryException(String table, String action) {
9
+        super();
10
+        this.table = table;
11
+        this.action = action;
12
+    }
13
+
14
+}

+ 14
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoNoVerionInfoException.java Целия файл

@@ -0,0 +1,14 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoNoVerionInfoException extends RuntimeException{
4
+
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoNoVerionInfoException(String table, String action) {
9
+        super();
10
+        this.table = table;
11
+        this.action = action;
12
+    }
13
+
14
+}

+ 15
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/JpaDaoOptimisticLockingFailureException.java Целия файл

@@ -0,0 +1,15 @@
1
+package ltass.staudy.spring.data.jpa.exception;
2
+
3
+public class JpaDaoOptimisticLockingFailureException extends RuntimeException{
4
+    private String rawMessage;
5
+    private String table;
6
+    private String action;
7
+
8
+    public JpaDaoOptimisticLockingFailureException(String rawMessage, String table, String action) {
9
+        super();
10
+        this.rawMessage = rawMessage;
11
+        this.table = table;
12
+        this.action = action;
13
+    }
14
+
15
+}

Loading…
Отказ
Запис