smallflyelephant666 7 лет назад
Родитель
Сommit
fbfb5bdc5a

+ 8
- 0
src/main/java/ltass/staudy/spring/data/jpa/constant/Constants.java Просмотреть файл

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
 import com.alibaba.fastjson.JSON;
3
 import com.alibaba.fastjson.JSON;
4
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
4
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
5
 import ltass.staudy.spring.data.jpa.entity.User;
5
 import ltass.staudy.spring.data.jpa.entity.User;
6
+import ltass.staudy.spring.data.jpa.exception.JpaDaoException;
6
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
7
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
7
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.web.bind.annotation.GetMapping;
9
 import org.springframework.web.bind.annotation.GetMapping;
35
         try{
36
         try{
36
             userJpaDao.insert(user);
37
             userJpaDao.insert(user);
37
         }catch (Exception ex){
38
         }catch (Exception ex){
38
-            return dealException(ex);
39
+            return JSON.toJSONString(ex);
39
         }
40
         }
40
         return "OK.";
41
         return "OK.";
41
     }
42
     }
49
         try{
50
         try{
50
             userJpaDao.insert(user);
51
             userJpaDao.insert(user);
51
         }catch (Exception ex){
52
         }catch (Exception ex){
52
-            return dealException(ex);
53
+            return JSON.toJSONString(ex);
53
         }
54
         }
54
         return "OK.";
55
         return "OK.";
55
     }
56
     }
71
         user.setUpdateProcess("web");
72
         user.setUpdateProcess("web");
72
         try{
73
         try{
73
             userJpaDao.insert(user);
74
             userJpaDao.insert(user);
75
+        }catch (JpaDaoException ex){
76
+            return "jpadaoException";
77
+            //return JSON.toJSONString(ex);
74
         }catch (Exception ex){
78
         }catch (Exception ex){
75
-            return dealException(ex);
79
+            return "exception";
80
+            //return JSON.toJSONString(ex);
76
         }
81
         }
77
         return "OK.";
82
         return "OK.";
78
     }
83
     }
134
         try{
139
         try{
135
             userJpaDao.insert(user);
140
             userJpaDao.insert(user);
136
         }catch (Exception ex){
141
         }catch (Exception ex){
137
-            return dealException(ex);
142
+            return JSON.toJSONString(ex);
138
         }
143
         }
139
         return "OK.";
144
         return "OK.";
140
     }
145
     }
158
         try{
163
         try{
159
             userJpaDao.insert(user);
164
             userJpaDao.insert(user);
160
         }catch (Exception ex){
165
         }catch (Exception ex){
161
-            return dealException(ex);
166
+            return JSON.toJSONString(ex);
162
         }
167
         }
163
         return "OK.";
168
         return "OK.";
164
     }
169
     }
183
         try{
188
         try{
184
             userJpaDao.update(user);
189
             userJpaDao.update(user);
185
         }catch (Exception ex){
190
         }catch (Exception ex){
186
-            return dealException(ex);
191
+            return JSON.toJSONString(ex);
187
         }
192
         }
188
         return "OK.";
193
         return "OK.";
189
     }
194
     }
208
         try{
213
         try{
209
             userJpaDao.update(user);
214
             userJpaDao.update(user);
210
         }catch (Exception ex){
215
         }catch (Exception ex){
211
-            return dealException(ex);
216
+            return JSON.toJSONString(ex);
212
         }
217
         }
213
         return "OK.";
218
         return "OK.";
214
     }
219
     }
233
         try{
238
         try{
234
             userJpaDao.update(user);
239
             userJpaDao.update(user);
235
         }catch (Exception ex){
240
         }catch (Exception ex){
236
-            return dealException(ex);
241
+            return JSON.toJSONString(ex);
237
         }
242
         }
238
         return "OK.";
243
         return "OK.";
239
     }
244
     }
257
         try{
262
         try{
258
             userJpaDao.update(user);
263
             userJpaDao.update(user);
259
         }catch (Exception ex){
264
         }catch (Exception ex){
260
-            return dealException(ex);
265
+            return JSON.toJSONString(ex);
261
         }
266
         }
262
         return "OK.";
267
         return "OK.";
263
     }
268
     }
280
         try{
285
         try{
281
             userJpaDao.update(user);
286
             userJpaDao.update(user);
282
         }catch (Exception ex){
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
         return "OK.";
313
         return "OK.";
286
     }
314
     }
291
         try{
319
         try{
292
             userJpaDao.deleteById(8L);
320
             userJpaDao.deleteById(8L);
293
         }catch (Exception ex){
321
         }catch (Exception ex){
294
-            return dealException(ex);
322
+            return JSON.toJSONString(ex);
295
         }
323
         }
296
         return "OK.";
324
         return "OK.";
297
     }
325
     }
302
         try{
330
         try{
303
             userJpaDao.deleteById(1L);
331
             userJpaDao.deleteById(1L);
304
         }catch (Exception ex){
332
         }catch (Exception ex){
305
-            return dealException(ex);
333
+            return JSON.toJSONString(ex);
306
         }
334
         }
307
         return "OK.";
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
 package ltass.staudy.spring.data.jpa.dao.impl;
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
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
5
 import ltass.staudy.spring.data.jpa.dao.UserJpaDao;
4
 import ltass.staudy.spring.data.jpa.entity.User;
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
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
10
 import ltass.staudy.spring.data.jpa.repo.UserRepository;
11
+import ltass.staudy.spring.data.jpa.utils.IdUtils;
6
 import org.springframework.beans.factory.annotation.Autowired;
12
 import org.springframework.beans.factory.annotation.Autowired;
7
 import org.springframework.stereotype.Service;
13
 import org.springframework.stereotype.Service;
8
 
14
 
9
 @Service
15
 @Service
10
-public class UserJpaDaoImpl implements UserJpaDao {
16
+public class UserJpaDaoImpl implements UserJpaDao{
11
     @Autowired
17
     @Autowired
12
     UserRepository repository;
18
     UserRepository repository;
13
 
19
 
14
     @Override
20
     @Override
15
     public User insert(User entity) {
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
     @Override
44
     @Override
20
     public User update(User entity) {
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
     @Override
63
     @Override

+ 68
- 0
src/main/java/ltass/staudy/spring/data/jpa/exception/ExceptionHelper.java Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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
+}

Загрузка…
Отмена
Сохранить