Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 冬眠。将带有外键的对象保存到数据库。MySQLIntegrityConstraintViolationException:列不能为null_Java_Spring_Hibernate - Fatal编程技术网

Java 冬眠。将带有外键的对象保存到数据库。MySQLIntegrityConstraintViolationException:列不能为null

Java 冬眠。将带有外键的对象保存到数据库。MySQLIntegrityConstraintViolationException:列不能为null,java,spring,hibernate,Java,Spring,Hibernate,在将新对象保存到Users表时,我在这个方法调用中有MySQLIntegrityConstraintViolationException sessionFactory.getCurrentSession.save(newUser); 堆栈跟踪: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.C

在将新对象保存到
Users
表时,我在这个方法调用中有MySQLIntegrityConstraintViolationException

sessionFactory.getCurrentSession.save(newUser);
堆栈跟踪:


org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

root cause

org.hibernate.exception.ConstraintViolationException: could not execute statement
    org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
    org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
    org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
    org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
    org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    com.petrez.model.dao.GenericDaoImpl.put(GenericDaoImpl.java:28)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy30.put(Unknown Source)
    com.petrez.service.RegistrationServiceImpl.registerUser(RegistrationServiceImpl.java:21)
    com.petrez.controller.Index.processRegistration(Index.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'roleId' cannot be null
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    com.mysql.jdbc.Util.getInstance(Util.java:386)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
    org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
    org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
    org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
    org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    com.petrez.model.dao.GenericDaoImpl.put(GenericDaoImpl.java:28)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy30.put(Unknown Source)
    com.petrez.service.RegistrationServiceImpl.registerUser(RegistrationServiceImpl.java:21)
    com.petrez.controller.Index.processRegistration(Index.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
我在MySQL中有3个表<代码>用户,
角色
帐户

用户
表中有
角色的外键
User.roleId->Roles.roleId
一对多。主键
userId

Accounts
表具有
Users
的外键:
Accounts.accountId->Users.userId
一对一。主键
accountId

c创建新用户对象

@Override
    public void registerUser(Registration reg) {
        User newUser = new User();
        newUser.setUsername(reg.getEmail());
        newUser.setPassword(reg.getPassword());

        genericDao.put(newUser);
        Integer userId = genericDao.getByName(User.class, "username", reg.getEmail()).get(0).getUserId();
        genericDao.put(new Account(userId));
    }
我在
genericDao.put(newUser)上有异常方法调用。此方法中只有
sessionFactory.getCurrentSession().save(obj)
角色
我不创建的对象。因此
User.account
User.role
为空。也在MySQL中配置了默认值

User.java


@Entity
@SuppressWarnings("unused")
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class User implements Serializable, UserDetails{

    private static final long serialVersionUID = 6475100100480917262L;

    @Id
    @Column(name = "userId", length = 10)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

    @JoinColumn(name = "roleId", nullable = true, updatable = false)
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    private Role role;

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Account account;

    @Column(name = "username", length = 30)
    private String username;

    @Column(name = "password", length = 45)
    private String password;

    @Column(name = "nonExpired")
    private boolean accountNonExpired;

    @Column(name = "nonLocked")
    private boolean accountNonLocked;

    @Column(name = "credentialsNonExpired")
    private boolean credentialsNonExpired;

    @Column(name = "enabled")
    private boolean enabled;

    //Getter and setters
}

@Entity
@SuppressWarnings("unused")
@Table(name = "Roles")
public class Role implements Serializable, GrantedAuthority, Cloneable {

    private static final long serialVersionUID = 3586090230433600805L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "roleId", length = 10, unique = true, nullable = false)
    private Integer roleId;


    @Column(name = "role", length = 45, unique = true, nullable = true)
    private String  role;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "role")
    private Set usetSet = new HashSet(0);

    //Getters and setters
}

@Entity
@Table(name = "Accounts")
@SuppressWarnings("unused")
public class Account implements Serializable {

    private static final long serialVersionUID = -7045300306954075788L;

    @Id
    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @Parameter(name = "property", value = "user"))
    @GeneratedValue(generator = "generator")
    @Column(name = "accountId", length = 10, nullable = false)
    private Integer accountId;

    @Column(name = "firstName", length = 45, nullable = true)
    private String firstName;

    @Column(name = "lastName", length = 45, nullable = true)
    private String lastName;

    @Column(name = "gender", nullable = true)
    private Boolean gender;

    @Temporal(TemporalType.DATE)
    @Column(name = "birthDate", nullable = true)
    private Date birthDate;

    @Column(name = "profilePhoto", length = 250, nullable = false)
    private String profilePhoto;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    private User user;

    //Getters and setters
}
Role.java


@Entity
@SuppressWarnings("unused")
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class User implements Serializable, UserDetails{

    private static final long serialVersionUID = 6475100100480917262L;

    @Id
    @Column(name = "userId", length = 10)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

    @JoinColumn(name = "roleId", nullable = true, updatable = false)
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    private Role role;

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Account account;

    @Column(name = "username", length = 30)
    private String username;

    @Column(name = "password", length = 45)
    private String password;

    @Column(name = "nonExpired")
    private boolean accountNonExpired;

    @Column(name = "nonLocked")
    private boolean accountNonLocked;

    @Column(name = "credentialsNonExpired")
    private boolean credentialsNonExpired;

    @Column(name = "enabled")
    private boolean enabled;

    //Getter and setters
}

@Entity
@SuppressWarnings("unused")
@Table(name = "Roles")
public class Role implements Serializable, GrantedAuthority, Cloneable {

    private static final long serialVersionUID = 3586090230433600805L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "roleId", length = 10, unique = true, nullable = false)
    private Integer roleId;


    @Column(name = "role", length = 45, unique = true, nullable = true)
    private String  role;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "role")
    private Set usetSet = new HashSet(0);

    //Getters and setters
}

@Entity
@Table(name = "Accounts")
@SuppressWarnings("unused")
public class Account implements Serializable {

    private static final long serialVersionUID = -7045300306954075788L;

    @Id
    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @Parameter(name = "property", value = "user"))
    @GeneratedValue(generator = "generator")
    @Column(name = "accountId", length = 10, nullable = false)
    private Integer accountId;

    @Column(name = "firstName", length = 45, nullable = true)
    private String firstName;

    @Column(name = "lastName", length = 45, nullable = true)
    private String lastName;

    @Column(name = "gender", nullable = true)
    private Boolean gender;

    @Temporal(TemporalType.DATE)
    @Column(name = "birthDate", nullable = true)
    private Date birthDate;

    @Column(name = "profilePhoto", length = 250, nullable = false)
    private String profilePhoto;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    private User user;

    //Getters and setters
}
Account.java


@Entity
@SuppressWarnings("unused")
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class User implements Serializable, UserDetails{

    private static final long serialVersionUID = 6475100100480917262L;

    @Id
    @Column(name = "userId", length = 10)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

    @JoinColumn(name = "roleId", nullable = true, updatable = false)
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    private Role role;

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Account account;

    @Column(name = "username", length = 30)
    private String username;

    @Column(name = "password", length = 45)
    private String password;

    @Column(name = "nonExpired")
    private boolean accountNonExpired;

    @Column(name = "nonLocked")
    private boolean accountNonLocked;

    @Column(name = "credentialsNonExpired")
    private boolean credentialsNonExpired;

    @Column(name = "enabled")
    private boolean enabled;

    //Getter and setters
}

@Entity
@SuppressWarnings("unused")
@Table(name = "Roles")
public class Role implements Serializable, GrantedAuthority, Cloneable {

    private static final long serialVersionUID = 3586090230433600805L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "roleId", length = 10, unique = true, nullable = false)
    private Integer roleId;


    @Column(name = "role", length = 45, unique = true, nullable = true)
    private String  role;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "role")
    private Set usetSet = new HashSet(0);

    //Getters and setters
}

@Entity
@Table(name = "Accounts")
@SuppressWarnings("unused")
public class Account implements Serializable {

    private static final long serialVersionUID = -7045300306954075788L;

    @Id
    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @Parameter(name = "property", value = "user"))
    @GeneratedValue(generator = "generator")
    @Column(name = "accountId", length = 10, nullable = false)
    private Integer accountId;

    @Column(name = "firstName", length = 45, nullable = true)
    private String firstName;

    @Column(name = "lastName", length = 45, nullable = true)
    private String lastName;

    @Column(name = "gender", nullable = true)
    private Boolean gender;

    @Temporal(TemporalType.DATE)
    @Column(name = "birthDate", nullable = true)
    private Date birthDate;

    @Column(name = "profilePhoto", length = 250, nullable = false)
    private String profilePhoto;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    private User user;

    //Getters and setters
}

对于这个类,我在任何地方都使用默认构造函数。

例外情况很明显:role列(在表Roles中)可能不为null。给定堆栈跟踪,我假设您正在保存一个与角色有关联的用户,而这个角色有一个null
role
字段。由于您将关联配置为级联,因此也会保存该角色,并且由于该角色具有null
角色
,因此插入失败

编辑:新错误显示数据库中的列
Users.roleId
不能为空。因此,在保存用户之前,必须为其设置一个角色。在数据库中为此列指定默认值是无用的,因为只有在insert语句中未为此列指定值时,才会使用默认值。但是Hibernate将始终指定一个值。不管怎样,默认值是个坏主意,因为Hibernate不知道它们,因此如果不刷新它们,就无法将它们包含在实体中


旁注:如果不从实体上的注释生成数据库模式,则不应指定这些注释的可空、唯一、长度属性。尤其是当它们与数据库中的实际列定义相矛盾时。当列实际上不可为空时,用
nullable=true
注释列会使事情变得非常混乱。

例外情况非常清楚:角色列(在表角色中)可能不为空。给定堆栈跟踪,我假设您正在保存一个与角色有关联的用户,而这个角色有一个null
role
字段。由于您将关联配置为级联,因此也会保存该角色,并且由于该角色具有null
角色
,因此插入失败

编辑:新错误显示数据库中的列
Users.roleId
不能为空。因此,在保存用户之前,必须为其设置一个角色。在数据库中为此列指定默认值是无用的,因为只有在insert语句中未为此列指定值时,才会使用默认值。但是Hibernate将始终指定一个值。不管怎样,默认值是个坏主意,因为Hibernate不知道它们,因此如果不刷新它们,就无法将它们包含在实体中


旁注:如果不从实体上的注释生成数据库模式,则不应指定这些注释的可空、唯一、长度属性。尤其是当它们与数据库中的实际列定义相矛盾时。当列实际上不可为空时,用
nullable=true
注释列会使事情变得非常混乱。

例外情况非常清楚:角色列(在表角色中)可能不为空。给定堆栈跟踪,我假设您正在保存一个与角色有关联的用户,而这个角色有一个null
role
字段。由于您将关联配置为级联,因此也会保存该角色,并且由于该角色具有null
角色
,因此插入失败

编辑:新错误显示数据库中的列
Users.roleId
不能为空。因此,在保存用户之前,必须为其设置一个角色。在数据库中为此列指定默认值是无用的,因为只有在insert语句中未为此列指定值时,才会使用默认值。但是Hibernate将始终指定一个值。不管怎样,默认值是个坏主意,因为Hibernate不知道它们,因此如果不刷新它们,就无法将它们包含在实体中


旁注:如果不从实体上的注释生成数据库模式,则不应指定这些注释的可空、唯一、长度属性。尤其是当它们与数据库中的实际列定义相矛盾时。当列实际上不可为空时,用
nullable=true
注释列会使事情变得非常混乱。

例外情况非常清楚:角色列(在表角色中)可能不为空。给定堆栈跟踪,我假设您正在保存一个与角色有关联的用户,而这个角色有一个null
role
字段。由于您将关联配置为级联,因此也会保存该角色,并且由于该角色具有null
角色
,因此插入失败

编辑:新错误显示数据库中的列
Users.roleId
不能为空。因此,在保存用户之前,必须为其设置一个角色。在数据库中为此列指定默认值是无用的,因为只有在insert语句中未为此列指定值时,才会使用默认值。但是Hibernate将始终指定一个值。默认值是个坏主意,因为Hibernate不是