2017-08-15 118 views
1

我有一个名为的contentPath,有可能有同样类型的父实体,以及同类型的儿子,有类似表示:春数据休息多对多树投影映射

@Id 
    @Column(name = "ID") 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    @Column(name = "NAME", length = 50) 
    @NotNull 
    private String name; 

    @Column(name = "DESCRIPTION") 
    private String description; 

    @ManyToOne 
    @JoinColumn(name="CONTENT_PATH_ID") 
    public ContentPath contentPath; 

    @OneToMany(mappedBy="contentPath") 
    public Set<ContentPath> contentPaths; 

    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(
      name = "ACTIVITY_CONTENT_PATH", 
      joinColumns = {@JoinColumn(name = "CONTENT_PATH_ID", referencedColumnName = "ID")}, 
      inverseJoinColumns = {@JoinColumn(name = "ACTIVITY_ID", referencedColumnName = "ID")}) 
    private Set<Activity> activities; 

我有我的ContentPathRepository将它公开为一个API。

@RepositoryRestResource(collectionResourceRel = "contentPaths", path = "contentPaths", excerptProjection = ContentPathProjection.class) 
public interface ContentPathRestRepository extends PagingAndSortingRepository<ContentPath, Long> { 

} 

和我的投影,它应该正确格式化我的对象。

@Projection(name = "contentPathProjection", types = ContentPath.class) 
public interface ContentPathProjection { 
    Long getId(); 
    String getName(); 
    Set<ContentPath> getContentPaths(); 
} 

我期待得到ContentPaths,其中有内部的ContentPaths的名单,我得到了成功,但它并不带来的ID,它只是把名称和描述,这是很奇怪,因为我的投影没有说明。

电流响应:

"name": "BOOK 1", 
"id": 1, 
"contentPaths": [ 
    { 
     "name": "UNIT 1", 
     "description": "UNIT 1 description" 
    }, 
    { 
     "name": "UNIT 2", 
     "description": "UNIT 2 description" 
    } 
] 

为什么会发生?如何解决它?

+0

你对'ContentPath.id'公共的getter? –

+0

是的。 'public long getId(){return id;}' 但奇怪的是它也返回一个描述,但我没有在我的投影中的描述。 –

+0

它也返回第一级的ID,但不是第二级。 –

回答

1

这是SDR的正常行为。它默认不显示id。要打开这个上只登记这样一个bean:

@Bean 
public RepositoryRestConfigurerAdapter repositoryRestConfigurerAdapter() { 
    return new RepositoryRestConfigurerAdapter() { 
     @Override 
     public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
      config.exposeIdsFor(ContentPath.class); 
      super.configureRepositoryRestConfiguration(config); 
     } 
    }; 
} 

关于description - 你有这样的领域:

@Column(name = "DESCRIPTION") 
private String description; 
+0

它解决了这个问题,但我想明白为什么?我怎样才能揭露其他领域,如关系? –

+0

@LuizMitidiero看看:https://spring.io/understanding/HATEOAS和https://speakerdeck.com/olivergierke/spring-data-rest-repositories-meet-hypermedia – Cepr0

+0

@LuizMitidiero ...和这个:https: //speakerdeck.com/olivergierke/hypermedia-apis-with-spring-5 – Cepr0