【问题标题】:Spring Data REST endpoint disappears (hibernate conflict)Spring Data REST 端点消失(休眠冲突)
【发布时间】:2018-10-17 23:49:21
【问题描述】:

我正在尝试将我的项目更新为 spring boot 2.0.5 版本。这是 简单github project

基本部分:

@Entity
@Table(name = "placement")
public class Placement {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false)
    private Long id;

    private String name;

    @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
    private PlacementType type;

    //... getters and setters
}

@Entity
@Table(name = "placement_type")
public class PlacementType  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false)
    private Long id;

    private String name;
    //... getters and setters
}

@RepositoryRestResource(collectionResourceRel = "placement", path = "placement")
public interface PlacementRepository extends 
     PagingAndSortingRepository<Placement, Long> {}

@RepositoryRestResource(collectionResourceRel = "placementType", path = "placementType")
public interface PlacementTypeRepository extends 
     PagingAndSortingRepository<PlacementType, Long> {}

build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
    }

...

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-data-rest:${springBootVersion}")
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.3.6.Final'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.9.0'
    compile 'com.h2database:h2:1.4.196'
}

问题:

当我启动应用程序时,两个 api 端点都可以正常工作:

GET http://localhost:8090/api/placement/ => 200 works fine
GET http://localhost:8090/api/placementType => 200 works fine

但是在我从 Placement uri (@ManyToOne) 访问 PlacementType 后,我无法再访问 PlacementType 端点:

GET http://localhost:8090/api/placement/1/type => 200 works fine
GET http://localhost:8090/api/placementType => 404 (and default spring error screen)

没有错误日志,或者我的日志记录级别不正确,但我不知道为什么请求第二次被重定向到 SimpleUrlHandlerMapping。

 LOG from first GET http://localhost:8090/api/placementType => 200 
 02:38:56.076 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/api/placementType] 
 02:38:56.080 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /api/placementType 
 02:38:56.088 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/api/placementType] 
 02:38:56.114 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Last-Modified value for [/api/placementType] is: -1 
 Hibernate: select placementt0_.id as id1_1_, placementt0_.name as name2_1_ from placement_type placementt0_ limit ?
 02:38:56.559 [http-ni] DEBUG m.m.a.RequestResponseBodyMethodProcessor - Written [PagedResource { content: [Resource { content: com.varren.model.PlacementType@65e962bd, links: [<http://localhost:8090/api/placementType/1>;rel="self", <http://localhost:8090/api/placementType/1>;rel="placementType"] }], metadata: Metadata { number: 0, total pages: 1, total elements: 1, size: 20 }, links: [<http://localhost:8090/api/placementType{?page,size,sort}>;rel="self", <http://localhost:8090/api/profile/placementType>;rel="profile"] }] as "application/hal+json" using [org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter@446c8c72] 
 02:38:56.562 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
 02:38:56.563 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Successfully completed request 

                 GET http://localhost:8090/api/placement/1/type => 200 works fine
 LOG from second GET http://localhost:8090/api/placementType => 404 
 03:09:08.285 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/api/placementType] 
 03:09:08.286 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /api/placementType 
 03:09:08.287 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/api/placementType] 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - Matching patterns for request [/api/placementType] are [/**] 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - URI Template variables for request [/api/placementType] are {} 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - Mapping [/api/placementType] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@74eec640]]] and 1 interceptor 
 03:09:08.293 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Last-Modified value for [/api/placementType] is: -1 
 03:09:08.294 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
 03:09:08.294 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Successfully completed request 

还有一件事。大多数时候我都有这个错误,但有时一切都很好。我在 github 存储库中添加了第二个 @Entity,以表明有时 2 个相同实体中的一个有效而另一个无效,但也许我遗漏了一些明显的东西

【问题讨论】:

  • 很奇怪。我现在唯一要尝试的是将属性名称从PlacementType type; 更改为PlacementType placementType;。
  • @Patrick 是的,试过几次。还测试了几个java版本。切换到启动 2.0.6。经过几天的调试,我只是觉得使用最新版本的hibernate-entitymanager 并且从未尝试删除依赖关系非常愚蠢。

标签: java spring spring-mvc spring-boot spring-data-rest


【解决方案1】:

看起来问题出在已弃用的hibernate-entitymanager 中,或者至少是版本不匹配

compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.3.6.Final'

我花了几天时间才意识到不再需要它,我应该切换到hibernate-core。但是切换到最新的hibernate-core也产生了同样的问题,所以我必须完全删除依赖并让spring-boot-starter-data-jpa使用它的版本(5.2.17.Final引导2.0.5.RELEASE)。

使用RepositoryRestHandlerMapping.lookupHandlerMethod 中的断点对此进行调试,并注意到 RepositoryRestHandlerMapping 缓存中的 class com.varren.model.PlacementType$HibernateProxy 很奇怪。

正常(工作)版本如下所示(没有 HibernateProxy):https://i.stack.imgur.com/1V0FU.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2019-04-01
    • 2021-07-08
    相关资源
    最近更新 更多