私はこのSQL文を持っている:Hibernate基準で結合テーブルから特定のエンティティを選択する方法は?
select c.*
from tableA a
inner join tableB b on a.id = b.a_id
inner join tableC c on b.id = c.b_id
と私の基準は次のとおりです。
Criteria criteria = session.createCriteria(tableA.class, "a")
.createAlias("a.b", "b")
.createAlias("b.c", "c");
私は基準をTABLEC選択することができますか?以下のような
何か -
List<tableC> listC = criteria.list()
は私が
.setProjection(Property.forName("c"))
投影を追加しようとしたが、私は次のエラーを得た:
org.hibernate.QueryException: could not resolve property: c of: ...xx.xx.tableA
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1465)
これは私のクラスである:
CnTermsGroupクラス
@Entity
@Table(name="CN_TERMS_GROUP")
public class CnTermsGroup extends implements Serializable {
private static final long serialVersionUID = 1145853863134597537L;
@Id
@Column(name="CN_TERMS_GROUP_ID")
private long cnTermsGroupId;
@Column(name="CN_TERMS_GROUP_NAME")
private String cnTermsGroupName;
@OneToMany(mappedBy="cnTermsGroup", cascade=CascadeType.ALL, orphanRemoval=true)
@Fetch(FetchMode.SUBSELECT)
private List<CnTermsCounted> cnTermsCounteds;
public CnTermsGroup() {
}
public long getCnTermsGroupId() {
return this.cnTermsGroupId;
}
public void setCnTermsGroupId(long cnTermsGroupId) {
this.cnTermsGroupId = cnTermsGroupId;
}
public String getCnTermsGroupName() {
return this.cnTermsGroupName;
}
public void setCnTermsGroupName(String cnTermsGroupName) {
this.cnTermsGroupName = cnTermsGroupName == null ? "" : cnTermsGroupName;
}
public List<CnTermsCounted> getCnTermsCounteds() {
return this.cnTermsCounteds;
}
public void setCnTermsCounteds(List<CnTermsCounted> cnTermsCounteds) {
this.cnTermsCounteds = cnTermsCounteds;
}
@Override
public Object getId(){
return cnTermsGroupId;
}
}
CnTermsCountedクラス
@Entity
@Table(name="CN_TERMS_COUNTED")
public class CnTermsCounted extends BaseModelImp implements Serializable{
private static final long serialVersionUID = -8486592249097766983L;
@Id
@Column(name="CN_TERMS_COUNTED_ID")
private long cnTermsCountedId;
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name="CN_TERMS_GROUP_ID")
private CnTermsGroup cnTermsGroup;
@OneToMany(mappedBy = "cnTermsCounted", cascade=CascadeType.ALL, orphanRemoval=true)
@Fetch(FetchMode.SUBSELECT)
private Set<CnTermsRun> cnTermsRuns;
public CnTermsCounted() {
}
public long getCnTermsCountedId() {
return this.cnTermsCountedId;
}
public void setCnTermsCountedId(long cnTermsCountedId) {
this.cnTermsCountedId = cnTermsCountedId;
}
public CnTermsGroup getCnTermsGroup() {
return this.cnTermsGroup;
}
public void setCnTermsGroup(CnTermsGroup cnTermsGroup) {
this.cnTermsGroup = cnTermsGroup;
}
public Set<CnTermsRun> getCnTermsRuns() {
return this.cnTermsRuns;
}
public void setCnTermsRuns(Set<CnTermsRun> cnTermsRuns) {
this.cnTermsRuns = cnTermsRuns;
}
@Override
public Object getId() {
return cnTermsCountedId;
}
}
とCnTermsRunクラス
@Entity
@Table(name="CN_TERMS_RUN")
public class CnTermsRun extends BaseModelImp {
private static final long serialVersionUID = -9042920700902106238L;
@Id
@Column(name="CN_TERMS_RUN_ID")
private long cnTermsRunId;
@ManyToOne
@JoinColumn(name="CN_TERMS_COUNTED_ID")
private CnTermsCounted cnTermsCounted;
public CnTermsRun() {
}
public long getCnTermsRunId() {
return this.cnTermsRunId;
}
public void setCnTermsRunId(long cnTermsRunId) {
this.cnTermsRunId = cnTermsRunId;
}
public CnTermsCounted getCnTermsCounted() {
return this.cnTermsCounted;
}
public void setCnTermsCounted(CnTermsCounted cnTermsCounted) {
this.cnTermsCounted = cnTermsCounted;
}
/* (non-Javadoc)
* @see com.sintecmedia.onair.model.imp.BaseModelImp#getId()
*/
@Override
public Object getId() {
return cnTermsRunId;
}
}
と基準:
Criteria criteria = session.createCriteria(CnTermsGroup.class, "cnTermsGroup")
.createAlias("cnTermsGroup.cnTermsCounteds", "cnTermsCounted")
.createCriteria("cnTermsCounted.cnTermsRuns")
.setResultTransformer(Transformers.aliasToBean(CnTermsRun.class));
は、なぜあなたは基準条件= session.createCriteria(tableC.class、 "C")から起動することはできませんか? – StanislavL
私はJPAクラスで持っている '@Fetch(FetchMode.SUBSELECT)'アノテーションを使用して、ヒットをDBに減らしたいと考えています。 – Mie
.setResultTransformer(Transformers.aliasToBean(tableC.class))はどうですか? – StanislavL