简体   繁体   English

Spring Data Projection不起作用

[英]Spring Data Projection doesn't work

It looks like Spring-Data projections don't works properly. 看来Spring-Data投影无法正常工作。 Also the @CreationDate annotated fields are nulls. @CreationDate注释字段也为空。 What's wrong with that? 怎么了

This is Category entity class: 这是类别实体类:

package api.product.domain;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Set;

    @Entity
    @Getter
    @DynamicUpdate
    @EqualsAndHashCode(of = "name")
    @NoArgsConstructor
    class Category {

        @Id
        @GeneratedValue(generator = "ID_GENERATOR")
        private Long id;

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "category", cascade = CascadeType.ALL)
        private Set<Product> products;

        @Setter
        @Accessors(chain = true)
        @Column(unique = true, length = 20, nullable = false)
        private String name;

        @ManyToOne(cascade = {CascadeType.ALL})
        @JoinColumn(name = "parent_category_id")
        @Setter
        @Accessors(chain = true)
        private Category parentCategory;

        @CreatedDate
        @Setter
        private LocalDateTime creationDate;

        @LastModifiedDate
        @Setter
        private LocalDateTime lastModifiedDate;

        Category(String name, Category parentCategory){
            this.name = name;
            this.parentCategory = parentCategory;
        }
    }

The projection's interface: 投影的界面:

public interface ShowCategoryDto {
    @Value("#{target.id}")
    Long getId();
    String getName();
    Set<ProductBasicDto> getProducts();
}

And Repository: 和存储库:

interface CategoryRepository extends Repository<Category, Long> {

    Set<ShowCategoryDto> findAll();

    Optional<ShowCategoryDto> findOptionalById(Long id);

    Category save(Category category);

    Optional<Category> getOneById(Long id);

    void removeById(Long id);
}

Now when I call the findAll method it returns: 现在,当我调用findAll方法时,它将返回:

creationDate:null
id:1
lastModifiedDate:null
name:"wwwwwwwwww"
parentCategory:null
products:[]

So I see two problems here. 所以我在这里看到两个问题。 The first one is that projection isn't applied, and the second one is that there are nulls in the creationDate and lastModifiedDate fields. 第一个是不应用投影,第二个是在creationDatelastModifiedDate字段中为空。 Does someone know the reason why this happens and could share it with me? 有人知道发生这种情况的原因并可以与我分享吗?

The findAll is the method of CrudRepository interface, its signature is: findAll是CrudRepository接口的方法,其签名为:

List<T> findAll()

where T is your entity . 其中T是您的实体

If you need a custom method that return projections instead of the entities you should use another name for the method, for example: 如果您需要一个自定义方法来返回投影而不是实体,则应为该方法使用其他名称,例如:

List<MyProjection> findBy()
List<MyProjection> findAllBy()
List<MyProjection> findProjectionsBy()
List<MyProjection> getProjectionsBy()

etc. 等等

More info: 更多信息:
Query Creation 查询创建
Working with projections 处理投影

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

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