关于java:请求处理失败; 嵌套的异常是org.springframework.dao.DataIntegrityViolationException:无法执行语句;

Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement;

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

User.java

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

Role.java

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

这是角色表到role_user表和用户表到role_user表之间关系的图形表示
enter image description here

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

这是完整的堆栈跟踪

1
2
3
4
5
6
7
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)

增加更多的堆栈跟踪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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行的列'password'来说太长了"

在该示例中,您使用UserService在保存用户之前先对密码进行加密。

这里UserServiceImpl.class

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

尝试此链接以查看生成的加密密码,并相应地扩展列。

我希望这有帮助。