简体   繁体   中英

Spring-data-rest NullPointerException on OneToMany relationship

I'm developing an app with 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 .

I've reported this on the spring-data-rest Jira, and it's still unresolved. 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.

Following, the java classes:

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. 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.

On the other hand, I have never used the JPA annotation in the way that you do. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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