[英]How to join two tables with a group query using Hibernate (5.4) Criteria?
[英]how can i concatenate two properties into one property using hibernate criteria query
例如,有2個屬性門牌號和密碼,我想要一個屬性作為地址,如門牌號是10,pincode是110064和組合地址屬性是10,110064這是我的代碼
final Criteria criteria= getDatabaseSession().createCriteria(Application.class, "application");
final ProjectionList projectionList=Projections.projectionList();
criteria.setProjection(projectionList);
projectionList.add(Projections.property("address.street"), "street");
projectionList.add(Projections.property("address.postcode"), "postcode");
projectionList.add(Projections.property("address.houseNumber"), "houseNumber");
criteria.createAlias("application.applicationCase", "applicationCase", JoinType.INNER_JOIN);
criteria.createAlias("applicationCase.property", "property");
criteria.createAlias("property.address", "address");
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
return (Map<String, Object>) criteria.uniqueResult();
我想做這樣的事情
projectionList.add(Projections.property("address.street"+"address.houseNumber"+"address.postcode"),"address");
有人可以幫忙嗎
使用HQL
您可以使用concat
表達式,但它只能與HQL一起使用
select concat(address.street, address.houseNumber, address.postcode) as fullAddress from ...
使用@Formula
如果要使用Criteria,
可以使用@Formula
。 需要向持久性添加其他屬性
@Formula(value = "concat(f_street, f_houseNumber, f_postcode)")
private String fullAddress;
您需要指定列名(而不是屬性名),因為Hibernate會將它們按原樣添加到SQL中。 使用連接時不太方便 - 您需要在公式中指定由Hibernate生成的別名。
您可以參考Projection
的fullAddress
projectionList.add(Projections.property("fullAddress"), "fullAddress");
我用MySQl測試了它。 對於Oracle,您可以嘗試使用
@Formula(value = "f_street || f_houseNumber || f_postcode")
private String fullAddress;
擴展Hibernate
我試圖擴展Projection
以將concat
函數添加到Criteria
。 我測試它是最簡單的情況。
public class ConcatProjection extends SimpleProjection {
private static final String CONCAT_FUNCTION_NAME = "concat";
private final String[] properties;
public ConcatProjection(String... properties) {
this.properties = properties;
}
@Override
public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
throws HibernateException {
String result = getFunction(criteriaQuery).render(StringType.INSTANCE,
propertiesToColumns(criteria, criteriaQuery), criteriaQuery.getFactory());
return result + " as y" + loc + '_';
}
private List<String> propertiesToColumns(Criteria criteria, CriteriaQuery criteriaQuery) {
List<String> result = new ArrayList<String>(properties.length);
for (String property : properties) {
result.add(criteriaQuery.getColumn(criteria, property));
}
return result;
}
private SQLFunction getFunction(CriteriaQuery criteriaQuery) {
return criteriaQuery.getFactory().getSqlFunctionRegistry()
.findSQLFunction(CONCAT_FUNCTION_NAME);
}
@Override
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new Type[] { StringType.INSTANCE };
}
}
運用
projectionList.add(
new ConcatProjection("address.street",
"address.houseNumber", "address.postcode"), "fullAddress");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.