JPQL GROUP BY不像SQL GROUP BY那样工作

时间:2011-12-14 13:21:57

标签: java jpql entitymanager

我有这两个方面:

产品 - 多对多 - >过滤(单向)

我试着用jqpl:

做这个sql语句
SQL:  SELECT * FROM product   JOIN filter   GROUP BY product.id;

JPQL: SELECT p FROM Product p JOIN p.filter GROUP BY p.id

SQL完美无缺。 JPQL为每个过滤器返回产品,例如

product1有两个过滤器:我从psql获得此结果:

结果列表: [0] product1 [1] product1

我如何得到与sql相同的结果?

@Entity
public abstract class Filter<E> implements Serializable{

private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private Long id;

public E getValue() {
    return value;
}

public void setValue(E value) {
    this.value = value;
}

private E value;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}   

}

@Entity
public class Product implements Serializable{

private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private long id;

@OneToMany(mappedBy = "product", cascade = {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
private Collection<AllocationPlan> allocationPlan;

@ManyToOne
private Vendor vendor;

@Size(min = 2, max = 50)
private String title;

@Size(min = 0, max = 1000)
private String description;

@Size(min = 0, max = 200)
private String mainPicturePath;

private boolean isActive;

@ManyToMany
private Collection<Filter<?>> filter;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Collection<AllocationPlan> getAllocationPlan() {
    return allocationPlan;
}

public void setAllocationPlan(Collection<AllocationPlan> allocationPlan) {
    this.allocationPlan = allocationPlan;
}

public Vendor getVendor() {
    return vendor;
}

public void setVendor(Vendor vendor) {
    this.vendor = vendor;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getMainPicturePath() {
    return mainPicturePath;
}

public void setMainPicturePath(String mainPicturePath) {
    this.mainPicturePath = mainPicturePath;
}

public boolean isActive() {
    return isActive;
}

public void setActive(boolean isActive) {
    this.isActive = isActive;
}

@Override
public boolean equals(Object obj){
    return ((obj instanceof Product) && 
            id == ((Product)obj).getId());
}

public Collection<Filter<?>> getFilter() {
    return filter;
}

public void setFilter(Collection<Filter<?>> param) {
    this.filter = param;
}

}

0 个答案:

没有答案