gpt4 book ai didi

java - Spring Data Rest 多对多树投影映射

转载 作者:行者123 更新时间:2023-11-30 06:33:11 27 4
gpt4 key购买 nike

我有一个名为 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"
}
]

为什么会发生这种情况?怎么解决?

最佳答案

这是 SDR 的正常行为。默认情况下它不显示id。要打开此功能,只需注册一个 bean,如下所示:

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

关于描述 - 您有此字段:

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

关于java - Spring Data Rest 多对多树投影映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45685348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com