简体   繁体   English

Spring Boot 从模型中提取有限的字段

[英]Spring boot extracting limited fields from a model

I am using JPA and below is my Dto for AssetItem.我正在使用 JPA,下面是我的 AssetItem Dto。 Here in assetItemDocsCollection i am trying to fetch Collection.在 assetItemDocsCollection 中,我正在尝试获取 Collection。 But i dont want all fields from AssetItemDocs.但我不想要 AssetItemDocs 中的所有字段。 For example i dont want documentByte as this field will be huge.例如,我不想要 documentByte,因为这个字段会很大。 I just want all fields leaving this field, how can i do this in less code.我只希望所有字段都离开该字段,我怎样才能用更少的代码做到这一点。

public class AssetItemDto extends AbstractDto<Long> {

    private Collection<AssetItemDocs> assetItemDocsCollection;
}

public class AssetItemDocs implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Lob
    @Column(name = "document_byte")
    private byte[] documentByte;
    @Column(name = "creation_date")
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    @JoinColumn(name = "asset_item_id", referencedColumnName = "id")
    @ManyToOne
    private AssetItem assetItemId;
}

This is typically what projections are made for: retrieving only part of the entity's attributes.这通常是进行投影的目的:仅检索实体的部分属性。 There are several ways to do it, but for instance:有几种方法可以做到这一点,例如:

Instead of having AssetItemDocs entities in the Collection of your DTO, create another DTO with all fields of AssetItemDocs that you want to fetch (that is all except documentByte ).不要在 DTO 的CollectionAssetItemDocs实体,而是创建另一个 DTO,其中包含要获取的AssetItemDocs的所有字段(除了documentByte之外的所有字段)。 That DTO must have a constructor with all theses fields as parameters (type and name must match those declared in the entity).该 DTO 必须有一个将所有这些字段作为参数的构造函数(类型和名称必须与实体中声明的相匹配)。

Then in your repository, write a query method whose return type is a Collection of that DTO.然后在您的存储库中,编写一个查询方法,其返回类型是该 DTO 的Collection If your query is derivable by its method name, it will work as is, but if your method is written using a JPQL query in a @Query annotation, you'll need to explicitly call the DTO constructor in the select cause with this kind of syntax: SELECT new full.package.MyDTO(entity.attribute1, entity.attribute2) FROM MyEntity entity .如果您的查询可通过其方法名称派生,它将按原样工作,但如果您的方法是使用@Query注释中的 JPQL 查询编写的,则需要在select原因中使用这种方式显式调用 DTO 构造函数语法: SELECT new full.package.MyDTO(entity.attribute1, entity.attribute2) FROM MyEntity entity

Then you just have to include the Collection returned by the repository in your AssetItemDto .然后,您只需将存储库返回的Collection包含在您的AssetItemDto中。

NB: although it would be possible to make a projection using the same entity class instead of a new DTO, I personally think it's a bad practice to expose entity objects in a DTO.注意:虽然可以使用相同的实体类而不是新的 DTO 进行投影,但我个人认为在 DTO 中公开实体对象是一种不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM