猿问

Spring Data REST 端点消失(休眠冲突)

我正在尝试将我的项目更新到 Spring Boot 2.0.5 版本。这是一个简单的github 项目


重要组成部分:


@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> {}

构建.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。


largeQ
浏览 172回答 1
1回答

慕森卡

看起来问题已被弃用hibernate-entitymanager或至少版本不匹配compile&nbsp;group:&nbsp;'org.hibernate',&nbsp;name:&nbsp;'hibernate-entitymanager',&nbsp;version:&nbsp;'5.3.6.Final'我花了几天才意识到不再需要它,我应该切换到hibernate-core.&nbsp;但是切换到最新的 hibernate-core 也产生了同样的问题,所以我必须完全删除依赖项并让spring-boot-starter-data-jpa使用它的版本(5.2.17.Final用于引导2.0.5.RELEASE)。用断点调试这个RepositoryRestHandlerMapping.lookupHandlerMethod并class com.varren.model.PlacementType$HibernateProxy在 RepositoryRestHandlerMapping 缓存中注意到奇怪。正常(工作)版本看起来像这样(没有 HibernateProxy):
随时随地看视频慕课网APP

相关分类

Java
我要回答