使用Jpa QueryDSL Projections Bean的一对多预测

时间:2017-12-15 05:44:37

标签: java spring jpa spring-data-jpa querydsl

我正在使用带有QueryDSL的JPA,在那种情况下,我将使用JpaQuery Projections从数据库中获取唯一需要的数据。在我的实体中有一个OneToMany(List)映射但是我无法在OneToMany映射字段(即List)上进行投影。请有人告诉我如何通过在OneToMany映射中使用投影来获取List。

我的实体类: -

public class Brand {
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = COLUMN_BRAND_ID)
    private int brandId;

    @Column(name = COLUMN_BRAND_NAME, columnDefinition = "VARCHAR(255)", nullable = false, unique = true)
    private String brandName;

    @OneToOne(cascade = CascadeType.MERGE )
    @JoinColumn(name = BRAND_LOGO_SMALL_FOREIGN_KEY)
    private File brandLogoSmall;

    @OneToOne(cascade =  CascadeType.MERGE)
    @JoinColumn(name = BRAND_LOGO_LARGE_FOREIGN_KEY)
    private File brandLogoLarge;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = COLUMN_CONTACT_PERSONS, nullable = true)
    private List<BrandContactPerson> contactPersons;

    @Column(name = COLUMN_OTHER_DETAILS, columnDefinition = "VARCHAR(255)", nullable = true)
    private String otherDetails;

    @Column(name = COLUMN_BRAND_STATUS, columnDefinition = "TINYINT(1) DEFAULT 1", nullable = false)
    private int status;

    @Column(name = BRAND_ADDED_BY_FOREIGN_KEY, nullable = true)
    private int createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = COLUMN_BRAND_ADDED_ON, columnDefinition = "DATETIME", nullable = false)
    private Date createdDate;

    @Column(name = BRAND_MODIFIED_BY_FOREIGN_KEY, nullable = true)
    private int updatedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = COLUMN_BRAND_MODIFIED_ON, columnDefinition = "DATETIME", nullable = true)
    private Date updatedDate;

    @OneToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = COLUMN_CLASSIFICATION)
    private Classification classification;

//Setter Getter

}

服务Impl类

@Repository
public class BrandCustomRepositoryImpl implements BrandCustomRepository {

    @Autowired
    private BrandQueryDslRepository brandQueryDslRepository;

    @Autowired
    private EntityManager em;

    @Override
    public List<Brand> search(BrandSearchModel searchQuery) {

        JPAQuery<Contract> query = new JPAQuery<>(em);


        QBrand qBrand = QBrand.brand;

        BooleanBuilder builder = new BooleanBuilder();
        if (searchQuery.getPageNo() == null) {
            searchQuery.setPageNo(0);
        }
        if (searchQuery.getPageSize() == null) {
            searchQuery.setPageSize(UserSearchModel.DEFAULT_PAGE_SIZE);
        }
        prepareBrandSearchBuilder(builder, qBrand, searchQuery);
        builder.and(qBrand.status.eq(Constant.ACTIVE));

        List<Brand> brand1 = query.from(qBrand.brand).where(builder).offset(0).limit(20)
                .select(Projections.bean(Brand.class, qBrand.brandName,qBrand.contactPersons))
                .fetch();



        return brand1;
    }

这是上面的例子。提前致谢。

0 个答案:

没有答案
相关问题