Mike
Mike

Reputation: 1031

springboot data rest not working with super class @MappedSuperclass

RESOLVED: guys, as @HatemJaber said, I just add to my preload inserts the version = 0 and everything works fine now. The version column cant contain null value

So I was doing some tests with springboot data rest and everything was working fine until I define my Entity class like this:

SuperClass:

@MappedSuperclass
public abstract class BaseEntity implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    // getters and setters and others @PrePersit @PreUpdate
}

Hero:

@Entity
public class Hero extends BaseEntity {
    // more boilerplate code ...
}

Find method works fine:

{
   _embedded: {
      heroes: [
        {
            createAt: "2016-08-13T16:57:22.099+0000",
            updateAt: null,
            name: "Spiderman",
            firstName: "Peter",
            lastName: "Parker",
            birthday: "1980-01-01T15:00:00.000+0000",
   _links: {
           self: {
           href: "http://localhost:8080/rest/heroes/1"
         },
        hero: {
           href: "http://localhost:8080/rest/heroes/1"
         }
    }
} 

This get by id returns empty page

http://localhost:8080/rest/heroes/1

and throwns a NullPointerException

java.lang.NullPointerException: null
at org.springframework.data.rest.webmvc.support.ETag.getVersionInformation(ETag.java:192) ~[spring-data-rest-webmvc-2.5.2.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.support.ETag.from(ETag.java:76) ~[spring-data-rest-webmvc-2.5.2.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.HttpHeadersPreparer.prepareHeaders(HttpHeadersPreparer.java:64) ~[spring-data-rest-webmvc-2.5.2.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:352) ~[spring-data-rest-webmvc-2.5.2.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.5.4.jar:8.5.4]enter code here

Is this a bug or I'm doing something wrong?

PS: just to make clear, everything works fine if I dont use the superclass

Upvotes: 1

Views: 1435

Answers (2)

Hatem Jaber
Hatem Jaber

Reputation: 2402

I'm going to take a stab at this and say that you may have used a data-sql file to load the database and that you did not include a version field?

That was my issue, I did a find and replace to add version and a value to the insert statements for the test data that I'm pre-loading and everything is working as expected.

Upvotes: 4

Mike
Mike

Reputation: 1031

Looks like spring-data-rest is not working well with Entities using @Version annotation or I'm missing some extra configuration.

Looks like a bug.

Upvotes: 1

Related Questions