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, 
} 

검색 상품 및 속성 별 상품 그룹입니다.

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

이 작업을 수행 할 예정입니까?

편집 : 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

왜 명시 적으로 _attributes_에 JOIN을 추가하지 않으시겠습니까? 그러면 해당 필드에 액세스 할 수 있습니까? 그것은 Attribute 필드를 참조 할 수 있어야합니다. 또한 SELECT 절에서 다중 값 필드를 사용할 수 없습니다. –

+0

쿼리 결과 예를 추가했습니다. 컬렉션 멤버 엔티티에 가입하면 쿼리 결과를 어떻게 표시 할 수 있습니까? 감사. – Dave

답변

0

당신은 콜렉션에 의해 그룹과 선택 절에 다중 값 필드를 선택할 수 없습니다.

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(); 
} 
} 
관련 문제