JPA查询根据一个条件按多列检索数据

时间:2018-03-26 09:09:34

标签: java mysql spring spring-data-jpa jpql

你好,我有像这样相互映射的实体。 物业实体

@Entity
@Table(name = "properties")
public class Property extends BaseStatusAuditEntity {

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "property")
    private List<Inventory> inventory;

    @JsonIgnore
    @ManyToOne
    private User user;


    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "property")
    @JsonIgnore
    private List<Transaction> transaction;

}

库存实体:

@Entity
@Table(name = "inventory")
public class Inventory extends BaseStatusAuditEntity {

    @ManyToOne
    private Property property;

    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "inventory")
    private List<Transaction> transaction;
}

最后是交易实体

@Entity
@Table(name = "transactions")
public class Transaction extends BaseStatusAuditEntity {
    @ManyToMany
    private List<Property> property;

    @ManyToMany
    private List<Inventory> inventory;
}

我有查询按属性检索事务,但它只获取数据严格只有事务属性,因为你可以看到库存也有属性。我想运行查询来从Transaction获取数据,其中属性匹配属性,库存属性匹配我给出的属性。 我的查询看起来像这样。

@Query("SELECT t FROM Transaction AS t JOIN t.property p WHERE p IN ?1 AND t.createdAt BETWEEN ?2 AND ?3 ")
List<Transaction> getAllByPropertyAndDatesBetweenGroupedByTransactionType(List<Property> property, Date dateFrom, Date dateTo);

如何检索事务属性在属性数组中的数据以及事务库存属性在属性数组中的数据。

0 个答案:

没有答案