相关疑难解决方法(0)

Spring JPA选择特定列

我正在使用Spring JPA来执行所有数据库操作.但是我不知道如何从Spring JPA中的表中选择特定的列?

例如:
SELECT projectId, projectName FROM projects

java jpa spring-data-jpa

119
推荐指数
9
解决办法
21万
查看次数

Spring数据jpa - 返回对象的最佳方法?

我有这样的对象:

@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和主题的对象数组.实现这一目标最好的方法是什么?

spring hibernate jpa jpql spring-data-jpa

0
推荐指数
1
解决办法
1892
查看次数

标签 统计

jpa ×2

spring-data-jpa ×2

hibernate ×1

java ×1

jpql ×1

spring ×1