我想向DB添加新用户。 当我创建db ID时,将其设置为autoincrement,而不是null(电子邮件,密码也是)
这是我的用户:
@Entity
@Table(name = "roles")
public class RoleDAO {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "role")
private String role;
@OneToMany(mappedBy="roleDAO")
private List<User> users;
用户与其他实体RoleDAO相关。所以它的代码是下一个
RoleDAO:
"exception": "org.springframework.orm.jpa.JpaSystemException",
"message": "could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement",
我有:
2018-05-04 15:09:57.775 ERROR 5012 --- [nio-8088-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause
java.sql.SQLException: Field 'role' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[mysql-connector-java-5.1.40.jar:5.1.40]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970) ~[mysql-connector-java-5.1.40.jar:5.1.40]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) ~[mysql-connector-java-5.1.40.jar:5.1.40]
at com.mysql.jdb
c.MysqlIO.sendCommand(MysqlIO.java:2524) ~[mysql-connector-java-5.1.40.jar:5.1.40]
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at com.sun.proxy.$Proxy108.save(Unknown Source) ~[na:na]
at com.edsson.expopromoter.api.service.UserService.create(UserService.java:52) ~[classes/:na]
at com.edsson.expopromoter.api.controller.AuthController.registration(AuthController.java:74) ~[classes/:na]
at com.edsson.expopromoter.api.controller.AuthController$$FastClassBySpringCGLIB$$a58f74db.invoke(<generated>) ~[classes/:na]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.6.jar:8.5.6]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_60]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.6.jar:8.5.6]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]
有什么想法吗?
已更新
RoleDAO roleDAO = roleService.findRoleDAOByRole(RolesConfiguration.ROLE_USER);
userService.create(new User(registrationRequest.getEmail(),registrationRequest.getPassword(), roleDAO ));
出现在
之后{{1}}
我添加了完整的堆栈跟踪。当我在userService.create
中调用userRepository.save时会出现这种情况一种解决方法
在db中为此列添加默认值。我的用户有默认值ROLE_User。所以我把它放进去了。
我仍然想知道其他决议