我遇到以下错误:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO user_role(username) VALUES(?);Column 'username' cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'username' cannot be null
org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:87)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:620)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:850)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:871)
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:317)
dao.UserDAOImpl.setPrivigiles(UserDAOImpl.java:42)
dao.UserDAOImpl.create(UserDAOImpl.java:34)
dao.UserDAOImpl.create(UserDAOImpl.java:1)
service.UserService.addUser(UserService.java:16)
controller.RegisterController.doPost(RegisterController.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.sql.SQLIntegrityConstraintViolationException: Column 'username' cannot be null
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:975)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1114)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1062)
com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1383)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1047)
org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:87)
org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:87)
org.springframework.jdbc.core.JdbcTemplate.lambda$update$0(JdbcTemplate.java:855)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:850)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:871)
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:317)
dao.UserDAOImpl.setPrivigiles(UserDAOImpl.java:42)
dao.UserDAOImpl.create(UserDAOImpl.java:34)
dao.UserDAOImpl.create(UserDAOImpl.java:1)
service.UserService.addUser(UserService.java:16)
controller.RegisterController.doPost(RegisterController.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
UserDAOImpl类代码下面:
package dao;
import java.util.List;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import model.User;
import util.ConnectionProvider;
public class UserDAOImpl implements UserDAO {
private static final String CREATE_USER =
"INSERT INTO user(username,email,password,is_active) VALUES(:username,:email,:password,:active);";
private NamedParameterJdbcTemplate template;
public UserDAOImpl() {
template = new NamedParameterJdbcTemplate(ConnectionProvider.getDataSource());
}
@Override
public User create(User user) {
User resultUser = new User(user);
KeyHolder holder = new GeneratedKeyHolder();
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
int update = template.update(CREATE_USER, paramSource, holder);
if (update > 0) {
resultUser.setId((holder.getKey()).longValue());
setPrivigiles(resultUser);
}
return resultUser;
}
private void setPrivigiles(User user) {
final String userRoleQuery = "INSERT INTO user_role(username) VALUES(:username);";
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
template.update(userRoleQuery, paramSource);
}
}
我创建了两个表:user和user_role,它们通过username参数作为外键绑定。我将添加更多的用户添加到数据库中,当我要将参数传递给第二个表:user_role时,错误弹出。此表中没有新内容。 有人可以帮我解决此异常吗?