springboot数据休息时没有使用超类@MappedSuperclass

时间:2016-08-13 17:08:32

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

已解决:伙伴们,正如@HatemJaber所说,我只是添加到我的预加载插入版本= 0,现在一切正常。版本列不能包含空值

所以我正在使用springboot数据休息进行一些测试,一切正常,直到我定义我的Entity类:

超类:

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

英雄:

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

查找方法工作正常:

{
   _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"
         }
    }
} 

此get by id返回空白页

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

并抛出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

这是一个错误还是我做错了什么?

PS:只是为了弄清楚,如果我不使用超类

,一切正常

2 个答案:

答案 0 :(得分:4)

我要对此进行一次尝试,并说您可能使用了data-sql文件来加载数据库并且您没有包含version字段?

这是我的问题,我做了一个查找和替换,将header("Access-Control-Allow-Origin: *"); Global $db; $db = new PDO('mysql:dbname=databasename;host=localhost', 'dbuser', 'dbpassword'); if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } // Access-Control headers are received during OPTIONS requests if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); exit(0); } $postdata = file_get_contents("php://input"); if (isset($postdata)) { $request = json_decode($postdata); $cart = $request->cart; $total_qty = $request->total_qty; $total_amount = $request->total_amount; } else { echo "Not called properly!"; } $query = $db->prepare(" INSERT INTO yourtable (cart, total_qty, total_amount) VALUES (:cart, :total_qty, :total_amount)"); $query->execute(array( ':cart' => $cart, ':total_qty' => $total_qty, ':total_amount' => $total_amount)); 和一个值添加到我预加载的测试数据的insert语句中,并且一切都按预期工作。

答案 1 :(得分:1)

看起来spring-data-rest与使用@Version注释的实体不兼容,或者我错过了一些额外的配置。

看起来像个错误。