투영되지 않고 그룹 : 대부분의 사람들은 볼 수 있습니다. 그러나 대부분의 사람들은 프로젝션을 사용하고 싶지 않습니다. 콩을 투영해야한다는 것입니다. (그리고 결과로 리턴됩니다). 아래 예제에서 나는 project
을 시도했는데 결과물로는 bean
이 필요합니다.
저는 약간의 트릭으로 같은 결과를 얻었습니다. 먼저 프로젝션없이 그룹을 적용하려고했지만 솔루션을 찾지 못했기 때문에 프로젝션에 의존해야합니다.여기
내가 내 엔티티 빈입니다
Parent
클래스로
p.*
를 원하고 나는 그것이 고유 원 자바 코드에서
select p.* FROM parent p INNER JOIN child c ON p.id_parent=c.id_father
WHERE c.child_name like '%?%' AND p.parent_name like '%?%'
group by p.id_parent
을 달성하고 싶었다 무엇 하나의 방법은에 결과 목록을 얻을 수있다 설정,하지만 난 여러 가지 이유로 이런 식으로하지 말아요 :)
그래서 Parent.class
대신 Child.class
에서 Criteria를 만들었습니다.이 트릭이 저에게 효과적이었습니다.
Criteria c = session.createCriteria(Child.class,"c");// starting from Child
c.add(Restrictions.like("childName", "abc", MatchMode.ANYWHERE));
c.createAlias("parent", "p"); //remember parent is an attribute in Child.class
c.add(Restrictions.like("p.parentName", "xyz", MatchMode.ANYWHERE));
c.setProjection(Projections.projectionList().add(Projections.groupProperty("parent"))); //projecting parent which is an attribute of Child.class
List<Parent> result = c.list(); //get the result
for (Parent p: result) {
System.out.println(p);
}
아직 매핑되지 않은 경우 매핑 된 Entity Bean 클래스가 여기에 있습니다.
package com.mazhar.beans;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "parent")
public class Parent {
private Integer idParent;
private String parentName;
private List<Child> childs;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_parent")
public Integer getIdParent() {
return idParent;
}
public void setIdParent(Integer idParent) {
this.idParent = idParent;
}
@Column(name = "parent_name")
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="parent", cascade=CascadeType.ALL)
public List<Child> getChilds() {
return childs;
}
public void setChilds(List<Child> childs) {
this.childs = childs;
}
}
내 자식 클래스
package com.mazhar.beans;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "child")
public class Child {
private Integer idChild;
private String childName;
private Parent parent; //this actually we projected in criteria query.
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_city", unique = true, nullable = false)
public Integer getIdChild() {
return idChild;
}
public void setIdChild(Integer idChild) {
this.idChild = idChild;
}
@Column(name = "city_name", nullable = false)
public String getChildName() {
return childName;
}
public void setChildName(String cName) {
this.childName = cName;
}
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id_father")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
'GROUP BY'는 정의상 데이터의 집계를 포함하므로 투영이 필요합니다. 테이블의 더 자세한 정보와 Hibernate가 생성하기를 원하는 SQL 질의를 추가했다면, 우리는 더 잘 권고 할 수있다. – skaffman
클래스 이벤트가 있습니다. 이 클래스에는 날짜 목록이 있습니다. (사실이 날짜들은 특수 클래스 인 DateWrapper로 Date를 감싸고 Id를 추가합니다. 왜냐하면 최대 절전 모드가 바로 값 유형의 콜렉션을 결합 할 수 없기 때문입니다). 나는 이벤트를 쿼리하고 x와 y 사이에 하나 이상의 이벤트가있는 모든 이벤트를 찾고 싶다. Criteria-API에 의해 생성되고 "GROUP BY id"를 추가 한 SQL-Query를 grap하면 정확하게 찾고있는 것입니다. 그러나 나는 GROUP BY를 추가하기 위해 Hibernate를 주장 할 방법을 찾을 수 없다! –