本文介绍了Spring数据休息一到很多级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JPA实体User和Authority,User和Parent都是孩子。用户和权限之间有一对多的关系。



用户 -

  @Data 
@Entity
@Table(name =users,uniqueConstraints = {@UniqueConstraint(columnNames = {username})})
public class User {

@Id
@CustomEmailValidator
@ Size.List({
@Size(min = 5,message =该字段必须至少为{min}个字符),
@Size(max = 100, message =该字段必须小于{max}个字符)
})
私有字符串用户名;

@Basic
@Column(name =enabled,columnDefinition =BIT,length = 1)
private boolean enabled;

@OneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE},mappedBy =user)
@Column(nullable = false)
private List< ;管理局> authority = new ArrayList<>();
}

权限 -

 @Entity 
@Table(name =authorities)
public class Authority {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@版本
私人整数版本;
$ b $ @NotNull
@ Size.List({
@Size(min = 5,message =该字段必须至少包含{min}个字符),
@Size(max = 100,message =该字段必须小于{max}个字符)
})
私有字符串权限;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =username,nullable = false)
private用户用户;
}

我将用户公开为Spring Data Rest存储库。当我做

我可以看到用于与权限一起列出。然而,当我尝试创建一个用户以及使用下面的json的权限 -

  {
username: [email protected]
enabled:true,
authorities:[
{

authority:ROLE_ADMIN



$ b $ / code $ / pre>

错误 -

  {
原因:{
原因:{
原因:空,
消息:列'用户名'不能为空
},
消息:无法执行语句
},
message:无法执行语句; SQL [n / a]; constraint [null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句

潜在的例外是 -

 造成者:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'用户名'不能为空
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)〜[na:1.8.0_40]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)〜 [na:1.8.0_40]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[na:1.8.0_40]
at java.lang.reflect.Constructor.newInstance(Constructor .java:422)〜[na:1.8.0_40]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)〜[mysql-connector-java-5.1.34.jar:5.1。 34]
at com.mysql.jdbc.Util.getInstance(Util.java:360)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql。 jdbc.SQLError.createSQLException(SQLError.java:971)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887 )〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)〜[mysql-connector-java-5.1 .34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.ConnectionImpl。 execSQL(ConnectionImpl.java:2530)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907)〜[mysql -connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141)〜[mysql-connector-java-5.1.34.jar: 5.1.34]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at com。 mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062)〜[mysql-connector-java-5.1.34.jar:5.1.34]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate (ResultSetReturnImpl.java:208)〜[hibernate-co重新4.3.8.Final.jar:4.3.8.Final]
... 151个常见框架省略
Hibernate:从Campaign中选择count(campaign0_.id)作为col_0_0_ campaign0_
Hibernate :选择campaign0_.id为id1_0_,campaign0_.campaignId为campaign2_0_,campaign0_.emailOfTheCreator为emailOfT3_0_,campaign0_.endDate为endDate4_0_,campaign0_.name为name5_0_,campaign0_.startDate为startDat6_0_,campaign0_.status为status7_0_,campaign0_.summary为summary8_0_, campaign0_.version与Campaign campaign0_ limit中的版本9_0_一样吗?
Hibernate:选择user0_.username为username1_3_1_,user0_.enabled为enabled2_3_1_,authoritie1_.username为username4_3_3_,authoritie1_.id为id1_2_3_,authoritie1_.id为id1_2_0_,authoritie1_.authority为authorit2_2_0_,authoritie1_.username为username4_2_0_,authoritie1_ .version version3_2_0_ from users user0_ left outer join authorities authoritie1_ on user0_.username = authoritie1_.username where user0_.username =?
Hibernate:插入用户(启用,用户名)值(?,?)
Hibernate:插入权限(权限,用户名,版本)值(?,?,?)
16: 48:14.260 [http-nio-8080-exec-1]警告ohejdbc.spi.SqlExceptionHelper -SQL错误:1048,SQLState:23000
16:48:14.267 [http-nio-8080-exec-1]错误ohejdbc.spi.SqlExceptionHelper - 列'username'不能为空
16:48:14.271 [http-nio-8080-exec-1]警告ohejdbc.spi.SqlExceptionHelper - SQL警告代码:1048,SQLState :23000
16:48:14.271 [http-nio-8080-exec-1] WARN ohejdbc.spi.SqlExceptionHelper - 列'username'不能为空
16:48:14.308 [http-nio -8080-exec-1]错误osdrwAbstractRepositoryRestController - 无法执行语句; SQL [n / a];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句
org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [n / a];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句
在org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:248)〜[spring-orm-4.1.5。 RELEASE.jar:4.1.5.RELEASE]
在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214)〜[spring-orm-4.1.5.RELEASE.jar:4.1 .5.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)〜[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework上的
org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)〜[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE] dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)〜[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.aop的org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)〜[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
。 framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)〜[spring-data-jpa-1.7.2.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 179)〜[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)〜[spring-aop -4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(Reflective MethodInvocation.java:179)〜[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)〜 [spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at com.sun.proxy。$ Proxy98.save(Unknown Source)〜[na:na]
at org .springframework.data.rest.core.invoke.CrudRepositoryInvoker.invokeSave(CrudRepositoryInvoker.java:106)〜[spring-data-rest-core-2.2.2.RELEASE.jar:na]
在org.springframework。 data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:414)〜[spring-data-rest-webmvc-2.2.2.RELEASE.jar:na]
at org.springframework.data.rest.webmvc .RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:232)〜[spring-data-rest-webmvc-2.2.2.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na :1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_4 0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) 〜[na:1.8.0_40]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)〜[spring-web-4.1.5.RELEASE.jar:4.1.5 .RELEASE]
在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)〜[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)〜[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)〜[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.web.servlet.mvc.method.annotation.Request MappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)〜[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:85)〜[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) 〜[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)〜[spring-webmvc-4.1 .5.RELEASE.jar:4.1.5.RELEASE]
在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)[spring-webmvc-4.1.5.RELEASE.jar:4.1 .5.RELEASE]
在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
在javax.servlet.http.HttpServlet.service(HttpServlet.java:644)[tomcat-embed-core-8.0.20.jar:8 .0.20]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( ApplicationFilterChain.java:291)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)[tomcat-embed -core-8.0.20.jar:8.0.20]
在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)[tomcat-embed-websocket-8.0.20.jar :8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)[tomcat-embed-core-8.0.20.jar:8.0.20]
在org.springframework.web。 filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter。 java:107)[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)[tomcat-embed -core-8.0.20.jar:8.0.20]
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)[tomcat-embed-core-8.0.20.jar:8.0 .20]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.web.access.intercept.FilterSecurityInterce ptor.doFilter(FilterSecurityInterceptor.java:84)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter( FilterChainProxy.java:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java: 113)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring -security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)[spring-security-web -3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security-web-3.2.3 .RELEASE.jar:3.2.3.RELEASE]
在org.springframework .security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web .FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter .doFilter(SecurityContextHolderAwareRequestFilter.java:154)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy .java:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45 )[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.ja va:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java: 140)[spring-security-oauth2-2.0.2.RELEASE.jar:na]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security-web -3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)[spring-security-web-3.2 .3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security-web-3.2.3.RELEASE .jar:3.2.3.RELEASE]
在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)[spring-security-web-3.2.3.RELEASE.jar:3.2 .3.RELEASE]
在org.springframework.web.filter.OncePe $ RepositoryFilter.doFilter(OncePerRequestFilter.java:107) java:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security -web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)[spring-security -web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[spring-web-4.1.5.RELEASE .jar:4.1.5.RELEASE]
org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE] $ or $ $ $ b $ org.springframework .security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy .doFilter(FilterChainProxy.java:160)[spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java :239)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)[tomcat-embed-core- 8.0.20.jar:8.0.20]
在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)[spring-web-4.1.5.RELEASE.jar:4.1.5。 RELEASE]
在org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107)[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)[ tomcat-embed-core-8.0.20.jar:8.0.20]
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)[tomcat-embed-core-8.0.20。 jar:8.0.20]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.authenticator.AuthenticatorBase .invoke(AuthenticatorBase.java:501)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)[ tomcat-embed-core-8.0.20.jar:8.0.20]
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)[tomcat-embed -core-8.0.20.jar:8.0.20]
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)[tomcat-embed-core-8.0.20.jar:8.0 .20]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org。 apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)[tomcat-embed-core-8.0.20.jar:8.0.20]
at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(
at org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223)[tomcat-embed-core-8.0.20.jar:8.0.20] [tomcat-embed-core-8.0.20.jar: -embed-core-8.0.20.jar:8.0.20]
在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1558)[tomcat-embed-core-8.0 .20.jar:8.0.20]
at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1515)[tomcat-embed-core-8.0.20.jar:8.0 .20]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_40]
at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8 .0_40]
at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.0.20.jar:8.0.20]
在java.lang.Thread.run(Thread.java:745)[na:1.8.0_40]
引起:org.hibernate.exception.ConstraintViolationException:无法执行语句
在org.hibernate .exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter。 (org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert)(SqlExceptionHelper.java :126)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.j dbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl .executeUpdate(ResultSetReturnImpl.java:211)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.id.IdentityGenerator $ GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java: 96)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)〜[hibernate-core -4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)〜[hibernate-core-4.3.8.Final。 jar:4.3.8.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final ]
在org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction .java:98)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)〜[ hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)〜[hibernate-core-4.3.8 .Final.jar:4.3.8.Final]
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)〜[hibernate-core-4.3.8.Final.jar:4.3。 8.Final]
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.event .internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performS ave(AbstractSaveEventListener.java:194)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125 )〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:73)〜[ hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:271)〜[hibernate-core-4.3.8 .Final.jar:4.3.8.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:251)〜[hibernate-core-4.3.8.Final.jar:4.3。 8.Final]
在org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:189)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java: 886)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868)〜[hibernate-core-4.3 .8.Final.jar:4.3.8.Final]
at org.hibernate.engine.spi.CascadingActions $ 6.cascade(CascadingActions.java:277)〜[hibernate-core-4.3.8.Final.jar :4.3.8.Final]
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org。 hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.engine.internal.Cascade .cascadeCollectionElements(Cascade.java:379)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java: 319)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.eng ine.internal.Cascade.cascadeAssociation(Cascade.java:296)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty (Cascade.java:161)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) 〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)〜[hibernate-core-4.3 .8.Final.jar:4.3.8.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:255)〜[hibernate-core-4.3.8.Final.jar: 4.3.8.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:317)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)〜[hibernate-core-4.3.8.Final.jar:4 .3.8.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
在org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.internal。 SessionImpl.merge(SessionImpl.java:858)〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863 )〜[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196)〜[hibernate-entitymanager- 4.3.8.Final.jar:4.3.8.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)〜[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497)〜[na:1.8.0_40]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java: 344)〜[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at com.sun.proxy。$ Proxy93.merge(Unknown Source)〜[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497)〜[na: 1.8.0_40]
at org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)〜[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at com.sun.proxy。$ Proxy93.merge(Unknown Source)〜[na:na]
at org.spr ingframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:410)〜[spring-data-jpa-1.7.2.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)〜[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl。 invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497)〜[na:1.8.0_40]
at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416)〜[spring-data-commons-1.9.2.RELEASE.jar:na]
at org.springframework .data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401)〜[spring-data-commons-1.9.2.RELEASE.jar:na]
在org.springframework.data。 repository.core.support.Repos itoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)〜[spring-data-commons-1.9.2.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java :179)〜[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport $ DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java: 486)〜[spring-data-commons-1.9.2.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.1 .5.RELEASE.jar:4.1.5.RELEASE]

请帮忙。我做错了什么?为什么hibernate在映射时抱怨用户名为空?



谢谢,
Vijay

解决方案

Finally I found the solution. It is an issue with Jackson json unmarshalling. What is missing is a way to tell the object mapper about the one to many relationship, only then it will create parent and child objects properly upon which hibernate handles the rest of the stuff. Luckily Jackson has two annotations @JsonManagedReference, @JsonBackReference to tell the object mapper about the relationship. Updated code -



User.java

@Data 
@Entity
@Table(name = \"users\", uniqueConstraints={ @UniqueConstraint(columnNames={\"username\"})})
public class User {

@Id
@CustomEmailValidator
@Size.List ({
@Size(min=5, message=\"The field must be at least {min} characters\"),
@Size(max=100, message=\"The field must be less than {max} characters\")
})
private String username;

@Basic
@Column(name = \"enabled\", columnDefinition = \"BIT\", length = 1)
private boolean enabled;

@@JsonManagedReference
@OneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE}, mappedBy = \"user\")
@Column(nullable = false)
private List<Authority> authorities = new ArrayList<>();
}

Authority.java

@Data 
@Entity
@Table(name =\"authorities\")
public class Authority {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@版本
私人整数版本;

@NotNull
@Size.List ({
@Size(min=5, message=\"The field must be at least {min} characters\"),
@Size(max=100, message=\"The field must be less than {max} characters\")
})
private String authority;

@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = \"username\", nullable=false)
private User user;
}

One more point if we use Set<> instead of List<> to hold the child objects, we should alter the equals and hash code methods properly as mentioned here -


I have two JPA entities User and Authority, User the parent and Authority is the child. There is one to many relationship between User and Authority.

User -

@Data
@Entity
@Table(name = "users", uniqueConstraints={ @UniqueConstraint(columnNames={"username"})})
public class User {

    @Id
    @CustomEmailValidator
    @Size.List ({
            @Size(min=5, message="The field must be at least {min} characters"),
            @Size(max=100, message="The field must be less than {max} characters")
    })
    private String username;

    @Basic
    @Column(name = "enabled", columnDefinition = "BIT", length = 1)
    private boolean enabled;

    @OneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE}, mappedBy = "user")
    @Column(nullable = false)
    private List<Authority> authorities = new ArrayList<>();
}

Authority -

@Data
@Entity
@Table(name ="authorities")
public class Authority {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Integer version;

    @NotNull
    @Size.List ({
            @Size(min=5, message="The field must be at least {min} characters"),
            @Size(max=100, message="The field must be less than {max} characters")
    })
    private String authority;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "username", nullable=false)
    private User user;
}

I have exposed User as a Spring Data Rest repository. When I dohttp://localhost/api/usersI am able to see uses to list along with Authorities. However when I try to create a user along with authorities using the following json -

{
   "username": "[email protected]",
   "enabled": true,
   "authorities": [
    {

       "authority": "ROLE_ADMIN"
    }
    ]

}

I get the following error -

{
    "cause": {
        "cause": {
            "cause": null,
            "message": "Column 'username' cannot be null"
        },
        "message": "could not execute statement"
    },
    "message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}

The underlying exception is -

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'username' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_40]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_40]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.Util.getInstance(Util.java:360) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:971) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062) ~[mysql-connector-java-5.1.34.jar:5.1.34]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    ... 151 common frames omitted
Hibernate: select count(campaign0_.id) as col_0_0_ from Campaign campaign0_
Hibernate: select campaign0_.id as id1_0_, campaign0_.campaignId as campaign2_0_, campaign0_.emailOfTheCreator as emailOfT3_0_, campaign0_.endDate as endDate4_0_, campaign0_.name as name5_0_, campaign0_.startDate as startDat6_0_, campaign0_.status as status7_0_, campaign0_.summary as summary8_0_, campaign0_.version as version9_0_ from Campaign campaign0_ limit ?
Hibernate: select user0_.username as username1_3_1_, user0_.enabled as enabled2_3_1_, authoritie1_.username as username4_3_3_, authoritie1_.id as id1_2_3_, authoritie1_.id as id1_2_0_, authoritie1_.authority as authorit2_2_0_, authoritie1_.username as username4_2_0_, authoritie1_.version as version3_2_0_ from users user0_ left outer join authorities authoritie1_ on user0_.username=authoritie1_.username where user0_.username=?
Hibernate: insert into users (enabled, username) values (?, ?)
Hibernate: insert into authorities (authority, username, version) values (?, ?, ?)
16:48:14.260 [http-nio-8080-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 23000
16:48:14.267 [http-nio-8080-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Column 'username' cannot be null
16:48:14.271 [http-nio-8080-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Warning Code: 1048, SQLState: 23000
16:48:14.271 [http-nio-8080-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - Column 'username' cannot be null
16:48:14.308 [http-nio-8080-exec-1] ERROR o.s.d.r.w.AbstractRepositoryRestController - could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:248) ~[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214) ~[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) ~[spring-data-jpa-1.7.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at com.sun.proxy.$Proxy98.save(Unknown Source) ~[na:na]
    at org.springframework.data.rest.core.invoke.CrudRepositoryInvoker.invokeSave(CrudRepositoryInvoker.java:106) ~[spring-data-rest-core-2.2.2.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:414) ~[spring-data-rest-webmvc-2.2.2.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:232) ~[spring-data-rest-webmvc-2.2.2.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) ~[spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) [spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:140) [spring-security-oauth2-2.0.2.RELEASE.jar:na]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_40]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_40]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.20.jar:8.0.20]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40]
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:73) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:271) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:251) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:189) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:886) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:255) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:317) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:858) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) ~[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at com.sun.proxy.$Proxy93.merge(Unknown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291) ~[spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at com.sun.proxy.$Proxy93.merge(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:410) ~[spring-data-jpa-1.7.2.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416) ~[spring-data-commons-1.9.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401) ~[spring-data-commons-1.9.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373) ~[spring-data-commons-1.9.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486) ~[spring-data-commons-1.9.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]

Please help. What I am doing wrong? Why hibernate complains the username is null when I have mapping? Is it possible that my json is not converted properly?

Thanks,Vijay

解决方案

Finally I found the solution. It is an issue with Jackson json unmarshalling. What is missing is a way to tell the object mapper about the one to many relationship, only then it will create parent and child objects properly upon which hibernate handles the rest of the stuff. Luckily Jackson has two annotations @JsonManagedReference, @JsonBackReference to tell the object mapper about the relationship. Updated code -

User.java

@Data
@Entity
@Table(name = "users", uniqueConstraints={ @UniqueConstraint(columnNames={"username"})})
public class User {

    @Id
    @CustomEmailValidator
    @Size.List ({
            @Size(min=5, message="The field must be at least {min} characters"),
            @Size(max=100, message="The field must be less than {max} characters")
    })
    private String username;

    @Basic
    @Column(name = "enabled", columnDefinition = "BIT", length = 1)
    private boolean enabled;

    @@JsonManagedReference
    @OneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE}, mappedBy = "user")
    @Column(nullable = false)
    private List<Authority> authorities = new ArrayList<>();
}

Authority.java

@Data
@Entity
@Table(name ="authorities")
public class Authority {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Integer version;

    @NotNull
    @Size.List ({
            @Size(min=5, message="The field must be at least {min} characters"),
            @Size(max=100, message="The field must be less than {max} characters")
    })
    private String authority;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "username", nullable=false)
    private User user;
}

One more point if we use Set<> instead of List<> to hold the child objects, we should alter the equals and hash code methods properly as mentioned here -Jackson bug (or feature!?) when using java.util.Set - mySet.size() is always 1

这篇关于Spring数据休息一到很多级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:53