Skip to content

Commit

Permalink
handle SQLIntegrityConstraintViolationException, Column cannot be nu…
Browse files Browse the repository at this point in the history
…ll (#47)
  • Loading branch information
sim-wangyan committed Apr 6, 2022
1 parent ed91ff8 commit 63ac444
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.*;

/**
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 63ac444

Please sign in to comment.