我正在使用Spring JPA来执行所有数据库操作.但是我不知道如何从Spring JPA中的表中选择特定的列?
例如:
SELECT projectId, projectName FROM projects
我有这样的对象:
@Entity
public class DocumentationRecord {
@Id
@GeneratedValue
private long id;
private String topic;
private boolean isParent;
@OneToMany
private List<DocumentationRecord> children;
...
}
Run Code Online (Sandbox Code Playgroud)
现在我想只获得主题和ID.有没有办法得到这样的格式:
[
{
id: 4234234,
topic: "fsdfsdf"
},...
]
Run Code Online (Sandbox Code Playgroud)
因为即使只使用此查询
public interface DocumentationRecordRepository extends CrudRepository<DocumentationRecord, Long> {
@Query("SELECT d.topic as topic, d.id as id FROM DocumentationRecord d")
List<DocumentationRecord> getAllTopics();
}
Run Code Online (Sandbox Code Playgroud)
我只能得到这样的记录:
[
[
"youngChild topic",
317
],
[
"oldChild topic",
318
],
[
"child topic",
319
],
]
Run Code Online (Sandbox Code Playgroud)
我不喜欢数组数组我想获得具有属性id和主题的对象数组.实现这一目标最好的方法是什么?