gpt4 book ai didi

java - 请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException : could not execute statement;

转载 作者:可可西里 更新时间:2023-11-01 09:02:40 25 4
gpt4 key购买 nike

我在这里跟进这个教程 http://hellokoding.com/registration-and-login-example-with-spring-xml-configuration-maven-jsp-and-mysql/使用 spring mvc 和 maven 创建注册和注册过程。我已经在角色表和用户表之间创建了映射到 user_role 的关系。一切似乎都很好,但当我尝试创建新用户时,应用程序会抛出错误。

用户.java

@ManyToMany
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
public Set<Role> getRoles() {
return roles;
}

角色.java

@ManyToMany(mappedBy = "roles")
public Set<User> getUsers() {
return users;
}

这是role表到role_user表和user表到role_user表之间关系的图示 enter image description here

请问为什么我会收到此错误 请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException:无法执行语句;

这是完整的堆栈跟踪

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

添加更多堆栈跟踪

root cause
org.hibernate.exception.DataException: could not execute statement
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:69)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)


root cause

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'password' at row 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3845)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)

当我尝试创建一个新用户时出现上面的堆栈跟踪

请问我哪里错了?非常感谢

最佳答案

问题很明确“第 1 行的‘密码’列数据太长”

在示例中,您使用 UserService 在保存用户之前加密密码。

这里是UserServiceImpl.class

@Override
public void save(User user) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user.setRoles(new HashSet<>(roleRepository.findAll()));
userRepository.save(user);
}

试试这个 link查看生成的加密密码并相应地扩展您的列。

希望对您有所帮助。

关于java - 请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException : could not execute statement;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com