2017-08-15 34 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