简体   繁体   English

JPQL 返回所有关联的实体

[英]JPQL returns all associated Entities

I am trying to get Order[] Array which includes all Orders where the associated document isn't received.我正在尝试获取 Order[] 数组,其中包含未收到相关文档的所有订单。

I tried this query and it returns the right number of rows.我试过这个查询,它返回正确的行数。

 @Query("Select o FROM Order o INNER JOIN o.properties p  INNER JOIN p.documents  d WHERE d.received = false")
    Order[] findUnreceivedOrders();

The problem is, the order objects in my array includes ALL documents not only the unreceived, but I want that the object only includes the unreceived document objects.问题是,我的数组中的订单对象不仅包括未接收的所有文档,而且我希望该对象仅包括未接收的文档对象。

Does anyone know how to solve this?有谁知道如何解决这个问题?

Thanks for help!感谢帮助!

Order.java订单.java

@Entity
@Table(name = "orders")
@JsonIgnoreProperties(ignoreUnknown = true,
        value = {"progress"})
public class Order {

    @Id
    @Column(name = "id", unique = true)
    private String orderid;
    @Column(name = "user_id")
    private String userid;
    @Column(name = "entrydate")
    private java.sql.Date entrydate;
    @Column(name = "info")
    private String info;
    @Column
    private boolean complete;
    @Column(name= "cached")
    private boolean cached;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "order")
    private List<Property> properties = new ArrayList<>();

    @OneToOne(mappedBy = "order", cascade =  CascadeType.ALL)
    private BillingAdress billingAdress;

    // Getter & Setter

Property.java属性.java

@Entity
@Table(name = "properties")
@JsonIgnoreProperties(ignoreUnknown = true,
        value = {"progress"})
public class Property
{
    @Id
    @Column(name = "propertyid", unique = true )
    private String id;
    @Column(name = "name")
    private String name;
    @Column(name = "street")
    private String street;
    @Column(name = "zip")
    private String zip;
    @Column(name = "town")
    private String town;
    @Column
    private String house_number;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "property")
    private List<Landregisterfolio> landregisterfolios = new ArrayList<>();

    @Column(name = "userid" )
    private String userid;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "property")
    private List<Document> documents = new ArrayList<>();

    @ManyToOne
    @JoinColumn(name = "order_id")
    @JsonIgnore
    private Order order;

    @Column(name = "order_id", insertable = false, updatable = false)
    private String orderid;

//Getter & Setter
}

Document.java文档.java


@Entity
@Table(name = "documents")
public class Document {

    @Id
    @Column(name="id")
    private String docid;
    @Column(name="name")
    private String docname;
    @Column(name = "received")
    private Boolean received;
    @Column(name = "requested")
    private Boolean requested;
    @Column(name ="last_contact")
    private Date lastContact;
    @Column(name ="intern_comment")
    private String intern_comment;
    @Column(name ="extern_comment")
    private String extern_comment;
    @Column(name="fees")
    private double fees;

    @ManyToOne
    @JoinColumn(name = "property_propertyid")
    @JsonIgnore
    private Property property;

    @Column(name = "property_propertyid", insertable = false, updatable = false)
    private String propertyid;

//Getter & Setter

}

Probably you can map @ManyToOne Order to your Document entity and after use for Order entity可能您可以将@ManyToOne Order映射到您的Document实体,并在用于Order实体之后

@JoinColumnOrFormula(formula = @JoinFormula("(select d.id from documents d WHERE d.order_id = id AND d.received = false"))
List<Document> unreceivedDocuments;

U have list of Property in Order and list of Document in Property . ü有列表PropertyOrder和名单DocumentProperty So if u have one Document with status not received in your list u will have this Order .因此,如果您有一份状态未在您的列表中收到的Document ,您将收到此Order

Thanks to @pdem !感谢@pdem!

Used "join fetch", changed my lists to sets and it works fine.使用了“join fetch”,将我的列表更改为集合,并且工作正常。

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

相关问题 (JPQL)检索不在@OneToMany属性中的所有实体 - (JPQL) Retrieve all entities which are not in @OneToMany property 关联实体和非关联实体之间的连接,导致在 JPQL 中使用 Lazy fetch 生成非持久实体不断抛出 JpqlSyntaxException - Join between associated and non associated entities, result into non persistent entity in JPQL with Lazy fetch keeps throwing JpqlSyntaxException 返回涉及多对多关系的实体的JPQL查询 - JPQL query that returns entities involving a many to many relationship Spring 数据 Jpa + Spring 使用@Query(本机和 JPQL)的投影为相关实体返回 Z37A6259CC0C1DAE29BDZ9A768 - Spring Data Jpa + Spring Projections using @Query (native and JPQL) returns null for related entities 使用JPQL从实体的ElementCollections中删除 - Delete from entities' ElementCollections with JPQL 检索JPQL,EclipseLink和MySQL中的实体 - Retrieving entities in JPQL, EclipseLink and MySQL JPQL-如何从其他实体集中选择任何行未引用的所有实体 - JPQL - how to select all entities not referred to by any row from other entity set 与多个实体关联的实体 - An entity that associated to multiple entities 使用AuditQuery获取关联的实体 - Fetching associated entities with AuditQuery 动态获取关联实体 - Dynamically fetch associated entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM