简体   繁体   English

关于一对多关系的Spring-data-rest NullPointerException

[英]Spring-data-rest NullPointerException on OneToMany relationship

I'm developing an app with spring-data-rest. 我正在开发具有spring-data-rest的应用程序。 On each OneToMany relationship I have, when I try to access the elements of the collection using the link on the One side, I get a NullPointerException . 在我拥有的每个OneToMany关系中,当我尝试使用一侧上的链接访问集合的元素时,都会收到NullPointerException

I've reported this on the spring-data-rest Jira, and it's still unresolved. 我已经在春季数据静止的Jira上报告了此问题,但仍未解决。 What I want to know is if someone has a OneToMany relationship that works properly on spring-data-rest, and the differences between the way the relationship is established on the working code and mine. 我想知道的是,是否有人在弹簧数据静止时具有可以正常工作的OneToMany关系,以及在工作代码和我的关系上建立关系的方式之间的差异。

Following, the java classes: 以下是java类:

package rest.model;

// Generated 04-dic-2013 16:39:39 by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Author generated by hbm2java
 */
@Entity
@Table(name = "AUTHOR", schema = "MPD_LD")
public class Author implements java.io.Serializable {

    private BigDecimal id;
    private String name;
    private Set<Book> books = new HashSet<Book>(0);

    public Author() {
    }

    public Author(BigDecimal id) {
        this.id = id;
    }

    public Author(BigDecimal id, String name, Set<Book> books) {
        this.id = id;
        this.name = name;
        this.books = books;
    }

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getId() {
        return this.id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    @Column(name = "NAME", length = 20)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "author")
    public Set<Book> getBooks() {
        return this.books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }

}


package rest.model;

// Generated 04-dic-2013 16:39:39 by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Book generated by hbm2java
 */
@Entity
@Table(name = "BOOK", schema = "MPD_LD")
public class Book implements java.io.Serializable {

    private String isbn;
    private Author author;
    private String title;

    public Book() {
    }

    public Book(String isbn) {
        this.isbn = isbn;
    }

    public Book(String isbn, Author author, String title) {
        this.isbn = isbn;
        this.author = author;
        this.title = title;
    }

    @Id
    @Column(name = "ISBN", unique = true, nullable = false, length = 20)
    public String getIsbn() {
        return this.isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID")
    public Author getAuthor() {
        return this.author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    @Column(name = "TITLE", length = 20)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

Here is the error I'm getting: 这是我得到的错误:

java.lang.NullPointerException
    at org.springframework.data.rest.webmvc.ResourceProcessorHandlerMethodReturnValueHandler.handleReturnValue(ResourceProcessorHandlerMethodReturnValueHandler.java:161)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:947)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:878)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:946)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:822)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

I think that the problem is in the FK of the relationship between the table BOOK and the table AUTHOR. 我认为问题出在表BOOK和表AUTHOR之间的关系中。 I mean, you are using the column ID, which is the PK of Author, in the @JoinColumn to create a relationship many to one between the entity Book and the entity Author when you should use the FK of the table BOOK with AUTHOR. 我的意思是,当您应该将表BOOK的FK与AUTHOR一起使用时,您在@JoinColumn中使用列ID(即Author的PK)在实体Book和实体Author之间创建多对一关系。

On the other hand, I have never used the JPA annotation in the way that you do. 另一方面,我从未以您使用的方式使用过JPA批注。 I usually put then over the attributes like this: 然后,我通常将这样的属性放在上面:

 @Entity
    @Table(name=”AUTHOR”, schema=”MPD_LD”)
    Public class Author implements java.io.Serializable {
        …
        @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY mappedBy=”author”)
        Private Set<Book> books = new HashSet<Book>(0);
        …
    }




 @Entity
    @Table(name=”BOOK”, schema=”MPD_LD”)
    Public class Author implements java.io.Serializable {
        …
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name=”<FK of AUTHOR>”
        Author author;
        …
    }

Have you tried to add a new book to an author? 您是否尝试过向作者添加新书? Does it work in your way? 它以您的方式工作吗?

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

相关问题 Spring-Data-Rest验证器 - Spring-Data-Rest Validator spring-data-rest和微服务:在另一个spring-data-rest服务中与@OneToOne关系实体的实体 - spring-data-rest and micro-services: Entity with @OneToOne relationship with Entity in another spring-data-rest service 使用 Spring-Data-Rest,如何从一个 REST 调用更新 OneToOne 关系双方的 FK? - Using Spring-Data-Rest, how to update FK on both sides of OneToOne relationship from one REST call? 使用Spring-data-rest中的Access-Control-Request-Method发出OPTIONS请求时发生NullPointerException - NullPointerException when making an OPTIONS request with Access-Control-Request-Method in Spring-data-rest 限制spring-data-rest返回的文档 - Restrict documents returned by spring-data-rest 丰富spring-data-rest存储库上的行为 - Enrich behavior on spring-data-rest repository 在spring-data-rest中阻止HTTP方法 - Prevents HTTP method in spring-data-rest 如何使用Spring Data REST在OneToMany关系中添加对象 - How to add an object in a OneToMany relationship using Spring Data REST 如何将 spring-data-rest 与 spring websocket 混合到一个实现中 - How to mix spring-data-rest with spring websocket into a single implementation spring-data-rest没有与spring-mvc完全集成 - spring-data-rest does not integrate cleanly with spring-mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM