Spring Data Rest 多对多树投影映射
Spring Data Rest Many to many tree projection mapping
我有一个名为 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);
}
};
}
关于 description
- 你有这个字段:
@Column(name = "DESCRIPTION")
private String description;
我有一个名为 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);
}
};
}
关于 description
- 你有这个字段:
@Column(name = "DESCRIPTION")
private String description;