关于OneToMany关系的Spring-data-rest NullPointerException

时间:2014-01-01 10:24:01

标签: java spring spring-data spring-data-jpa spring-data-rest

我正在开发一个带有spring-data-rest的应用程序。在我的每个OneToMany关系中,当我尝试使用One侧的链接访问集合的元素时,我得到NullPointerException

我已经在Spring-data-rest Jira上报告了这个问题,但它仍然没有得到解决。我想知道的是,如果某人有一个OneToMany关系在spring-data-rest上正常工作,以及在工作代码和我的关系建立关系的方式之间的差异。

以下是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;
    }

}

这是我得到的错误:

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)

1 个答案:

答案 0 :(得分:1)

我认为问题在于表BOOK和表AUTHOR之间关系的FK。我的意思是,你在@JoinColumn中使用列ID,即作者的PK,在你应该使用表BOOK的FK与AUTHOR时,在实体Book和实体Author之间创建多对一的关系。 / p>

另一方面,我从未像你那样使用JPA注释。我通常把这个属性放在这个:

 @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;
        …
    }

您是否尝试过向作者添加新书?它会以你的方式工作吗?