【问题标题】:Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement;请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException:无法执行语句;
【发布时间】:2016-08-18 04:50:32
【问题描述】:

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

User.java

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

Role.java

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

这是角色表到角色用户表和用户表到角色用户表之间关系的图形表示

请问为什么我会收到此错误Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement;

这是完整的堆栈跟踪

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)

当我尝试创建新用户时,会出现上述堆栈跟踪

请问我做错了什么?非常感谢

【问题讨论】:

  • 请添加完整的堆栈跟踪
  • @Jens 我正在这样做
  • @Jens 我已经添加了完整的堆栈跟踪
  • 这里你看到了原因:Data truncation: Data too long for column 'password' at row 1你的密码输入比列长

标签: java mysql spring maven spring-mvc


【解决方案1】:

问题很明确“第 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 以查看生成的加密密码并相应地扩展您的列。

我希望这会有所帮助。

【讨论】:

  • 那么你建议我在我的数据库上使用什么特定的 varchar 长度
  • 这里解释:stackoverflow.com/questions/5881169/… 和摘要 CHAR(60)
  • 我认为我的错误已经改变了 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'accounts.user_role' doesn't exist
  • 你检查你的数据库了吗?
  • 是的,桌子在那儿
猜你喜欢
  • 1970-01-01
  • 2018-05-11
  • 2013-05-06
  • 2021-12-21
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
相关资源
最近更新 更多