命名查询以从另一个表连接2个表

时间:2014-08-05 22:21:52

标签: hibernate hql named-query

我正在使用hibernate开发一个Java EE项目,我正面对一个类似于这个的类图:

my Classes

我想要的是订购产品的客户列表。这意味着我的命名查询必须获取所需产品的ID,并且我希望我的命名查询在Customer类上编写。

你可以帮我解决这个问题吗?

这些是我的课程:

@Entity
@Named Queries({
    /* NAMED QUERY THAT I M LOOKING FOR */
            })
public class customer implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_CUSTOMER", nullable = false)
    private Integer idCustomer;

    @Size(max = 255)
    @Column(name = "NAME", length = 255)
    private String name;    

    @Size(max = 255)
    @Column(name = "EMAIL", length = 255)
    private String email;    

    @JoinColumn(name = "ID_ORDER", referencedColumnName = "ID_ORDER", nullable = true)
    @ManyToOne(optional = false)
    private GroupeAccess idOrder;  
}

@Entity
public class order implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_ORDER", nullable = false)
    private Integer idOrder;

    private Date date;    

    @JoinTable(name = "order_product", joinColumns = {
        @JoinColumn(name = "ID_ORDER", referencedColumnName = "ID_ORDER", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "ID_PRODUCT", referencedColumnName = "ID_PRODUCT", nullable = false)})
    @ManyToMany
    private Collection<product> productCollection;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idCustomer")
    private Collection<customer> customerCollection;
}

@Entity
public class product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_PRODUCT", nullable = false)
    private Integer idProduct;

    private Date date;    

    @ManyToMany(mappedBy = "productCollection")
    private Collection<Acces> orderCollection;
}

1 个答案:

答案 0 :(得分:1)

Select o.customerCollection from Order o where :product in o.productCollection

相关问题