2016-08-05 3 views
0

Есть ли способ группировать по свойству коллекции? Например,JPA как сгруппировать по коллекционной собственности

public class Merchandise { 
    id, 
    name 

} 

public class Attribute { 
    id, 
    name, 
    value, 

    @ManyToOne 
    MerchandiseCost merchandiseCost; 
} 

public class MerchandiseCost { 
    Merchandise merchandise, 
    List<Attribute> attributes, 
    BigDecimal cost, 
} 

Поиск MerchandiseCost group по товарам и атрибутам.

select merchandise, attributes, sum(cost) from MerchandiseCost group by merchandise, attributes. 

Будет ли это работать?

EDIT: Если нет, то, как построить запрос, чтобы получить результаты, следующие с помощью CriteriaQuery API:

Merchandise   Attributes   SUM(COST) 
----------------------------------------------------------- 
Cloth   size:L, color:RED  10000 
Cloth   size:M, color:WHITE  20000 
Computer  Memory:4G    80000 
Computer  Memory:16G    90000 
+0

SQL, сам запрос выглядит нормально. Вы еще что-нибудь пробовали? –

+0

Почему бы не добавить JOIN в _attributes_ так, чтобы вы могли получить доступ к его полям? Это необходимо, чтобы иметь возможность ссылаться на поле атрибута. Также вы не можете использовать многозначное поле в предложении SELECT. –

+0

Добавлен пример результатов запроса. Если подключиться к члену элемента коллекции, как получить результаты запроса, как показано? Благодарю. – Dave

ответ

0

Вы не можете группировать по коллекции и не может выбрать многозначное поле в Select пункта.

Merchandise.class

@Embeddable 

public class Merchandise { 
private String name; 

public Merchandise() { 

} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (obj == this) { 
     return true; 
    } 
    if (obj.getClass() != getClass()) { 
     return false; 
    } 
    Merchandise rhs = (Merchandise) obj; 
    return new EqualsBuilder() 
      .append(this.name, rhs.name) 
      .isEquals(); 
} 

@Override 
public int hashCode() { 
    return new HashCodeBuilder() 
      .append(name) 
      .toHashCode(); 
} 
} 

Attribute.class

@Embeddable 
public class Attribute { 
private int id; 
private String name; 
private String value; 

private MerchandiseCost merchandiseCost; 

public int getId() { 
    return id; 
} 

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

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getValue() { 
    return value; 
} 



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

@ManyToOne 
public MerchandiseCost getMerchandiseCost() { 
    return merchandiseCost; 
} 

public void setMerchandiseCost(MerchandiseCost merchandiseCost) { 
    this.merchandiseCost = merchandiseCost; 
} 


@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (obj == this) { 
     return true; 
    } 
    if (obj.getClass() != getClass()) { 
     return false; 
    } 
    Attribute rhs = (Attribute) obj; 
    return new EqualsBuilder() 
      .append(this.id, rhs.id) 
      .append(this.name, rhs.name) 
      .append(this.value, rhs.value) 
      .append(this.merchandiseCost, rhs.merchandiseCost) 
      .isEquals(); 
} 

@Override 
public int hashCode() { 
    return new HashCodeBuilder() 
      .append(id) 
      .append(name) 
      .append(value) 
      .append(merchandiseCost) 
      .toHashCode(); 
} 
} 

MerchandiseCost.class

@Entity 
public class MerchandiseCost extends ABaseEntity { 
private Merchandise merchandise; 
private List<Attribute> attributes; 

private BigDecimal cost; 

@Embedded 
public Merchandise getMerchandise() { 
    return merchandise; 
} 

public void setMerchandise(Merchandise merchandise) { 
    this.merchandise = merchandise; 
} 

@ElementCollection 
@CollectionTable(name = "MERCHANDISE_ATTRIBUTE", joinColumns = @JoinColumn(name = "MERCHANDISE_ID")) 
public List<Attribute> getAttributes() { 
    return attributes; 
} 

public void setAttributes(List<Attribute> attributes) { 
    this.attributes = attributes; 
} 

public BigDecimal getCost() { 
    return cost; 
} 

public void setCost(BigDecimal cost) { 
    this.cost = cost; 
} 


@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (obj == this) { 
     return true; 
    } 
    if (obj.getClass() != getClass()) { 
     return false; 
    } 
    MerchandiseCost rhs = (MerchandiseCost) obj; 
    return new EqualsBuilder() 
      .append(this.merchandise, rhs.merchandise) 
      .append(this.attributes, rhs.attributes) 
      .append(this.cost, rhs.cost) 
      .isEquals(); 
} 

@Override 
public int hashCode() { 
    return new HashCodeBuilder() 
      .append(merchandise) 
      .append(attributes) 
      .append(cost) 
      .toHashCode(); 
} 
} 

MerchandiseResult.class

public class MerchandiseResult { 
private Merchandise merchandise; 
private Attribute attribute; 
private BigDecimal cost; 

public MerchandiseResult() { 
} 

public MerchandiseResult(Merchandise merchandise, Attribute attribute, BigDecimal cost) { 
    this.merchandise = merchandise; 
    this.attribute = attribute; 
    this.cost = cost; 
} 

public Merchandise getMerchandise() { 
    return merchandise; 
} 

public void setMerchandise(Merchandise merchandise) { 
    this.merchandise = merchandise; 
} 

public Attribute getAttribute() { 
    return attribute; 
} 

public void setAttribute(Attribute attribute) { 
    this.attribute = attribute; 
} 

public BigDecimal getCost() { 
    return cost; 
} 

public void setCost(BigDecimal cost) { 
    this.cost = cost; 
} 


@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (obj == this) { 
     return true; 
    } 
    if (obj.getClass() != getClass()) { 
     return false; 
    } 
    MerchandiseResult rhs = (MerchandiseResult) obj; 
    return new EqualsBuilder() 
      .append(this.merchandise, rhs.merchandise) 
      .append(this.attribute, rhs.attribute) 
      .append(this.cost, rhs.cost) 
      .isEquals(); 
} 

@Override 
public int hashCode() { 
    return new HashCodeBuilder() 
      .append(merchandise) 
      .append(attribute) 
      .append(cost) 
      .toHashCode(); 
} 
} 

MerchandiseDao.class

@Stateless 
public class MerchandiseDao { 
@PersistenceContext(name = "tngo") 
private EntityManager entityManager; 

public void readCost(){ 
    Query query = entityManager.createQuery("select NEW tngo.cert.training.model.MerchandiseResult(mc.merchandise, att, sum(mc.cost)) from MerchandiseCost mc join mc.attributes att group by mc.merchandise, att"); 
    query.getResultList(); 
} 
} 
Смежные вопросы