如何使用Spring JPA仅获取实体的选定属性? [英] How to fetch only selected attributes of an entity using Spring JPA?

查看:108
本文介绍了如何使用Spring JPA仅获取实体的选定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Spring Boot(1.3.3.RELEASE)和Hibernate JPA.我的实体看起来像这样:

I'm using Spring Boot (1.3.3.RELEASE) and Hibernate JPA in my project. My entity looks like this:

@Data
@NoArgsConstructor
@Entity
@Table(name = "rule")
public class RuleVO {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 128, nullable = false, unique = true)
    private String name;

    @Column(name = "tag", length = 256)
    private String tag;

    @OneToMany(mappedBy = "rule", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RuleOutputArticleVO> outputArticles;

    @OneToMany(mappedBy = "rule", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RuleInputArticleVO> inputArticles;
}

我的存储库如下:

@Repository
public interface RuleRepository extends JpaRepository<RuleVO, Long> {
}

在某些情况下,我只需要获取实体RuleVO的 id name 属性.我怎样才能做到这一点?我发现使用Criteria API和Projections应该可行,但怎么办?提前谢谢了. Vojtech

In some cases I need to fetch only id and name attributes of entity RuleVO. How can I achieve this? I found a notice it should be doable using Criteria API and Projections but how? Many thanks in advance. Vojtech

推荐答案

更新:

正如我所指出的,我很懒,而且可以很好地完成工作,因此,我在网上寻找合适的答案后会更新自己的答案.

As has been pointed out to me, I'm lazy and this can very well be done hence I'm updating my answer after having looked around the web for a proper one.

以下是如何获取id的 only 的名称的示例:

Here's an example of how to get only the id's and only the names:

@Repository
public interface RuleRepository extends JpaRepository<RuleVO, Long> {

    @Query("SELECT r.id FROM RuleVo r where r.name = :name") 
    List<Long> findIdByName(@Param("name") String name);

    @Query("SELECT r.name FROM RuleVo r where r.id = :id") 
    String findNameById(@Param("id") Long id);
}

希望此更新对您有所帮助

Hopefully this update proves helpful

旧答案:

仅检索特定属性的名称/id是不可能的,因为这不是spring的设计方式,也不是针对此问题的任何SQL数据库,因为您总是选择一行作为实体.

Only retrieving the specific attributes name/id is not possible as this is not how spring was designed or any SQL database for that matter as you always select a row which is an entity.

您可以做的是查询实体中的变量,例如:

What you CAN do is query over the variables in the entity, for instance:

@Repository
public interface RuleRepository extends JpaRepository<RuleVO, Long> {

    public RuleVo findOneByName(String name);
    public RuleVo findOneByNameOrId(String name, Long id);
    public List<RuleVo> findAllByName(String name);
    // etc, depending on what you want
}

您可以根据需要修改这些内容.您的需求.您可以直接通过自动关联的存储库调用这些方法

You can modify these however you want w.r.t. your needs. You can call these methods directly via the autowired repository

请参见 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/第5.3节提供了更多选项和示例

See http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ Section 5.3 for more options and examples

这篇关于如何使用Spring JPA仅获取实体的选定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆