gpt4 book ai didi

java - 如何从具有 Hibernate 标准的连接表中选择特定实体?

转载 作者:行者123 更新时间:2023-12-01 10:07:45 25 4
gpt4 key购买 nike

我有这个 SQL 语句:

 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)

这是我的类(class):

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));

最佳答案

我可以想到两种方法。

第一种方法是使用 Criteria.ALIAS_TO_ENTITY_MAP如下

Criteria criteria = session.createCriteria(CnTermsGroup.class, "cnTermsGroup")
.createAlias("cnTermsGroup.cnTermsCounteds", "cnTermsCounted")
.createAlias("cnTermsCounted.cnTermsRuns", "cnTermsRuns")
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

这将返回 List<Map<String, Object>> 。我们可以使用alias name收集所需的实体,如下,例如:到集合CnTermsRun实体:

List<CnTermsRun> subEntities = new ArrayList<>();
List<Map<String, Object>> objects = criteria.list();
for (Map<String, Object> record : objects) {
subEntities.add((CnTermsRun)record.get("cnTermsRuns"));
}
<小时/>

第二种方法是使用 Transformers.aliasToBean(...)如下

这将返回 List<CnTermsRun>直接但需要一个新的 constructor与默认构造函数一起添加:

public CnTermsRun(long cnTermsRunId, CnTermsCounted cnTermsCounted) {
this.cnTermsRunId = cnTermsRunId;
this.cnTermsCounted = cnTermsCounted;
}

以及以下标准:

Criteria criteria = session.createCriteria(CnTermsGroup.class, "cnTermsGroup")
.createAlias("cnTermsGroup.cnTermsCounteds", "cnTermsCounted")
.createAlias("cnTermsCounted.cnTermsRuns", "cnTermsRuns")
.setProjection(Projections.projectionList().add(Projections.alias(Projections.property("cnTermsRuns.id"), "cnTermsRunId")).add(Projections.alias(Projections.property("cnTermsRuns.cnTermsCounted"), "cnTermsCounted")))
.setResultTransformer(Transformers.aliasToBean(CnTermsRun.class));
List<CnTermsRun> subEntities = criteria.list();

关于java - 如何从具有 Hibernate 标准的连接表中选择特定实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com