繁体   English   中英

如何使用额外列保存具有多对多映射的对象?

[英]How to save object with many-to-many mapping with extra column?

如何使用额外列保存具有多对多映射的对象? 专栏是“批准”。 使用多对多注释我只调用了setUsersDocuments(用户),它工作正常......如何才能使用额外的列?

文献

@JsonIgnore
    @OneToMany(mappedBy = "document")
    private List<UsersDocuments> documentsForUsers;

用户类

@OneToMany(mappedBy = "user", orphanRemoval = true, cascade= CascadeType.ALL)
private List<UsersDocuments> usersDocuments;

PK

@Embeddable
public class UserDocumentsId implements Serializable {

    @Column(name="user_id")
    private long user;

    @Column(name="document_id")
    private long document;

很多桌子

    @Entity
@Table(name="users_documents")
public class UsersDocuments {

    @EmbeddedId
    private UserDocumentsId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("user")
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("document")
    @JoinColumn(name = "document_id")
    private Document document;

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

保存文件

Document document = new Document(title, desc);
        document.setUploadDatetime(new Date());
        document.setUser(user.get());

        List<User> users = userService.getUsersByRoles(roles);

//        document.setDocumentsForUsers(users); THIS WAS WHEN USING ONLY MANY TO MANY ANNOTATION

        documentService.saveDocument(document);

编辑:Unsuccesfull保存对象 - document.getDocumentsForUsers()。add(userDocuments)NULL。

Optional<User> user = userService.getUserByEmail(createdBy);
        Document document = new Document(title, desc);
        document.setUploadDatetime(new Date());
        document.setUser(user.get());

        List<User> users = userService.getUsersByRoles(roles);

        for(User userx : users){
            UsersDocuments userDocuments = new UsersDocuments();
            UserDocumentsId id = new UserDocumentsId();

            // Construct the Id with user and document
            id.setUser(userx.getId());
            id.setDocument(document.getId());

            userDocuments.setId(id);
            userDocuments.setApproval(true);

            // This is very important for saving and cascading to work.
            document.getDocumentsForUsers().add(userDocuments);
        }

        documentService.saveDocument(document);

编辑:错误

显示java.lang.NullPointerException:空在com.patrikmaryska.bc_prace.bc_prace.controller.DocumentController.getDocumentFromClient(DocumentController.java:85)〜[类/:NA]在com.patrikmaryska.bc_prace.bc_prace.controller.DocumentController $$ FastClassBySpringCGLIB $ 3a9de86b.invoke()〜[classes /:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)~ [spring-core-5.1.8.RELEASE.jar:5.1.8 .RELEASE]在com.patrikmaryska.bc_prace的org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)〜[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] .bc_prace.controller.DocumentController $$ EnhancerBySpringCGLIB $$ b46e5706.getDocumentFromClient()〜[classes /:na] at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:na] at java。 java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke中的base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:na](DelegatingM ethodAccessorImpl.java:43)〜[na:na] at java.base / java.lang.reflect.Method.invoke(Method.java:567)〜[na:na] at org.springframework.web.method.support。 InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)〜[弹簧网络5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138 )〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)〜[spring- webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)~ [spring-webmvc-5.1.8 .RELEASE.jar:5.1.8.RELEASE]在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)〜[spring-webmvc-5.1.8.RELEASE.jar: 5.1.8.RELEASE]在o org.springframework.web.servlet上的rg.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE] .dispatcherServlet.doDispatch(DispatcherServlet.java:1039)〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) 〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)〜[spring-webmvc-5.1.8.RELEASE .jar:5.1.8.RELEASE]在javax的org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.springframework.web.servlet.FrameworkServlet.service上的.servlet.http.HttpServlet.service(HttpServlet.java:660)〜[tomcat-embed-core-9.0.21.jar:9.0.21](FrameworkServlet.java: 882)〜[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]在javax.servlet.http.HttpServlet.s ervice(HttpServlet.java:741)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed -core-9.0.21.jar:9.0.21] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~ [tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)〜[tomcat-embed-websocket-9.0.21.jar:9.0.21] at org.apache.catalina.core.ApplicationFilterChain。 internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.21.jar:9.0.21]在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed -core-9.0.21.jar:9.0.21] at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)〜[spring-boot-actuator-2.1.6。 RELEASE.jar:2.1.6.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[sprin gg-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.21.jar :9.0.21] org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-9.0.21.jar:9.0.21] org.springframework.security.web在org.springframework.security.web.access.intercept.FilterSecurityInterceptor中的.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:320)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]。在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java)中调用(FilterSecurityInterceptor.java:127)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] :91)〜org.springframework.security.web.FilterChainProxy $ [VirtualFilterChain.doFilter]([...]在org.s的security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.security.web.FilterChainProxy中的pringframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter。 java:137)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring -security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)~ [spring-security-web-5.1。 5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE。 jar:5.1.5.RELEASE] at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE在org.springframework.security的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] .oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)〜[弹簧安全oauth2-2.3.4.RELEASE.jar:NA]在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)的FilterChainProxy.java:334)~ [spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] )org.springframework.security.web.FilterChainProxy $ [VirtualFilterChain.doFilter]([...] - [spring-securit] y-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)〜[spring-security-web-5.1.5 .RELEASE.jar:5.1.5.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在Org.springframework.security的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]。 web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]位于org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter。 java的:56 )〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[spring-web-5.1。 8.RELEASE.jar:5.1.8.RELEASE]在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:334)〜[spring-security-web-5.1.5.RELEASE.jar:5.1 .5.RELEASE] org.springframework上的org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate上的.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)〜[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] DelegatingFilterProxy.java:357)〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)~ [spring-web -5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.apache.catalina.core.ApplicationFilterChain。 internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.21.jar:9.0.21]在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed -core-9.0.21.jar:9.0.21] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)〜[spring-web-5.1.8.RELEASE.jar:5.1.8。发布在org.apache.catalina.core上的org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]。 ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~ [tomcat -embed-core-9.0.21.jar:9.0.21] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)〜[spring-web-5.1.8.RELEASE.jar:5.1。 8.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerR) equestFilter.java:109)〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed -core-9.0.21.jar:9.0.21] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~ [tomcat-embed-core-9.0.21.jar:9.0.21]在组织的org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)〜[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]。 org.springframework.web上的springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)〜[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE] .filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java: 193)〜[tomcat-embed-core-9.0.21.jar:9.0.21]在org.apache。 catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~ [tomcat-embed-core-9.0.21.jar:9.0.21] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200 )〜[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)〜[spring-web-5.1.8。在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-9.0.21.jar:9.0.21]的RELEASE.jar:5.1.8.RELEASE]。 apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~ [tomcat-embed-core-9.0.21.jar:9.0.21] at com.patrikmaryska.bc_prace.bc_prace.config.CorsConfig.doFilter(CorsConfig) .java:34)〜[classes /:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)~ [tomcat-embed-core-9.0.21.jar:9.0.21] at at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFi lterChain.java:166)~ [tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)〜[tomcat-embed-core -9.0.21.jar:9.0.21] org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org .apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve。 java:139)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)〜[tomcat-embed-core-9.0 。orj.:7.0.21]在org.apache的org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)〜[tomcat-embed-core-9.0.21.jar:9.0.21] .catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java: 408)〜[t omcat-embed-core-9.0.21.jar:9.0.21] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)〜[tomcat-embed-core-9.0.21.jar:9.0.21 ] org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:853)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1587)〜[tomcat-embed-core-9.0.21.jar:9.0.21] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 〜[tomcat-embed-core-9.0.21.jar:9.0.21]在java.base / java.base / java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)〜[na:na] java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628)〜[na:na] at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)〜[tomcat -embed-core-9.0.21.jar:9.0.21]在java.base / java.lang.Thread.run(Thread.java:835)〜[na:na]

1)在Document实体中添加级联到UserDocuments关系:

@OneToMany(mappedBy = "document", cascade= CascadeType.ALL)
private List<UsersDocuments> usersDocuments;

2)您需要以不同方式设置实体:

Document document = new Document(title, desc);
document.setUploadDatetime(new Date());

List<User> users = userService.getUsersByRoles(roles);

for(Use user: users){
    UserDocuments userDocuments = new UserDocuments();
    UserDocumentsId id = new UserDocumentsId();

    // Construct the Id with user and document
    id.setUser(user);
    id.setDocument(document);

    userDocuments.setDocumentsId(id);
    userDocuments.setActivtity(true);

    // This is very important for saving and cascading to work.
    document.getUserDocuments().add(userDocuments);
}

documentService.saveDocument(document);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM