From 63ac444f89f947b5243c94713497feda89e9a566 Mon Sep 17 00:00:00 2001 From: simsevenx Date: Wed, 6 Apr 2022 17:38:58 +0800 Subject: [PATCH] handle SQLIntegrityConstraintViolationException, Column cannot be null (#47) --- .../io/xream/sqli/repository/dao/DaoImpl.java | 10 +++++++ .../exception/SqliRumtimeException.java | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 sqli-repo/src/main/java/io/xream/sqli/repository/exception/SqliRumtimeException.java diff --git a/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java b/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java index dc632953..9b50d794 100644 --- a/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java +++ b/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java @@ -29,6 +29,7 @@ import io.xream.sqli.parser.BeanElement; import io.xream.sqli.parser.Parsed; import io.xream.sqli.parser.Parser; +import io.xream.sqli.repository.exception.SqliRumtimeException; import io.xream.sqli.repository.exception.TooManyResultsException; import io.xream.sqli.repository.init.SqlInit; import io.xream.sqli.repository.init.SqlTemplate; @@ -39,6 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.*; /** @@ -142,6 +144,14 @@ public boolean create(Object obj) { return this.jdbcHelper.create(isAutoIncreaseId,sql,valueList); } catch (Exception e) { + Throwable t = e.getCause(); + if ( t != null && + t instanceof SQLIntegrityConstraintViolationException){ + String msg = t.getMessage(); + if (msg.contains("cannot be null")) { + throw new SqliRumtimeException("Table of "+ Parser.get(clz).getTableName()+", " + msg); + } + } throw ExceptionTranslator.onRollback(obj, e, logger); } diff --git a/sqli-repo/src/main/java/io/xream/sqli/repository/exception/SqliRumtimeException.java b/sqli-repo/src/main/java/io/xream/sqli/repository/exception/SqliRumtimeException.java new file mode 100644 index 00000000..b5753730 --- /dev/null +++ b/sqli-repo/src/main/java/io/xream/sqli/repository/exception/SqliRumtimeException.java @@ -0,0 +1,29 @@ +/* + * Copyright 2020 io.xream.sqli + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.xream.sqli.repository.exception; + +/** + * @author Sim + */ +public class SqliRumtimeException extends RuntimeException{ + + public SqliRumtimeException(String message) { + super(message); + } +}