[英]HibernateCriteria left join with OR where clause
我們的一個查詢存在問題,該查詢返回的是內部聯接而不是左聯接。 該查詢旨在檢索用戶具有顯式訪問權限的所有項目(該用戶所屬的用戶組之一被定義為項目“用戶”用戶組)或隱式操作(該項目未定義“用戶”用戶組) 。
相關代碼:
// HibernateCriteria is a custom class that has junction, conjunction, disjunction objects (the ones from Hibernate)
ICriteria criteriaInProjectUserGroup = new HibernateCriteria();
criteriaInProjectUserGroup.addEqualTo(Project.USERGROUP+"." +UserGroup.USERGROUPASSOCIATIONS+"."+UserGroupAssociation.USEROID, user.getOid());
ICriteria criteriaNoProjectUserGroup = CriteriaFactory.createCriteria();
criteriaNoProjectUserGroup.addIsNull(Project.USERGROUP);
/*The join (criteriaIn...) must be added to the simple is null criteria (criteriaNo...),
* to result in a right outer join, otherwise an inner join is created by ojb which returns
* an empty result set */
criteriaNoProjectUserGroup.addOrCriteria(criteriaInProjectUserGroup);
IQuery query = QueryFactory.createQueryByCriteria(ProjectBean.class, criteriaNoProjectUserGroup);
Collection projects = broker.getCollectionByQuery(query);
addEqualTo定義為:
public void addEqualTo(String attribute, Object value) {
if (value!=null && value instanceof String && containsWildcards((String)value)) {
addLike(attribute, value);
}
else {
attribute = createAliasIfNecessary(attribute);
conjunction.add(eq(attribute, value));
}
}
addIsNull定義為:
public void addIsNull(String attribute) {
attribute = createAliasIfNecessary(attribute);
conjunction.add(isNull(attribute));
}
addOrCriteria定義為:
public void addOrCriteria(ICriteria criteria) {
if(criteria instanceof HibernateCriteria) {
HibernateCriteria subCriteria = (HibernateCriteria) criteria;
Junction subJunction = subCriteria.resolveJunction();
if(disjunction == null ) {
disjunction = disjunction();
}
disjunction.add(subJunction);
aliasNameSet.addAll(subCriteria.aliasNameSet);
}
}
resolveJunction定義為:
private Junction resolveJunction() {
if(junction == null) {
if(disjunction == null) { // no 'or' subcriteria have been added
junction = conjunction; // then the main junction is simply the conjunction
} else {
// disjunction != null means an 'or' subcriteria has been already added.
// then the main junction is disjunction with a conjunction as an operand
junction = disjunction;
junction.add(conjunction);
}
}
return junction;
}
getCollectionByQuery將該條件提取到org.hibernate.criterion.DetachedCriteria對象中:
try {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
// create aliases if necessary
Set<String> createdAliasSet = new HashSet<String>();
for(String aliasAssociationPath: aliasNameSet) {
String[] nestedPropertiesArray = aliasAssociationPath.split("\\.");
String previousNestedProperty = null;
for(String nestedProperty: nestedPropertiesArray) {
String currentAliasName =
(previousNestedProperty == null ? nestedProperty : previousNestedProperty + "_" + nestedProperty);
if(!createdAliasSet.contains(currentAliasName)) {
String currentAssociationPath =
(previousNestedProperty == null ? nestedProperty : previousNestedProperty + "." + nestedProperty);
detachedCriteria.createAlias(currentAssociationPath, currentAliasName);
createdAliasSet.add(currentAliasName);
}
previousNestedProperty = currentAliasName;
}
}
detachedCriteria.add(resolveJunction());
return detachedCriteria;
} catch (HibernateException e) {
throw new PersistenceBrokerException(e);
}
然后將其轉換為org.hibernate.Criteria對象,並在該函數上調用Criteria.list()。
產生的Mysql查詢為:
select
this_.oid as oid1_41_2_,
this_.archived as archived2_41_2_,
this_.archivedDateTime as archived3_41_2_,
this_.version as version4_41_2_,
this_.adminGroupOid as adminGro5_41_2_,
this_.buildScript as buildScr6_41_2_,
this_.buildToolType as buildToo7_41_2_,
this_.deployScript as deploySc8_41_2_,
this_.deployToolType as deployTo9_41_2_,
this_.description as descrip10_41_2_,
this_.issueTrackingSystemOid as issueTr11_41_2_,
this_.locked as locked12_41_2_,
this_.name as name13_41_2_,
this_.projectType as project14_41_2_,
this_.testScript as testScr15_41_2_,
this_.testToolType as testToo16_41_2_,
this_.userGroupOid as userGro17_41_2_,
this_.vcrOid as vcrOid18_41_2_,
this_.vcrProjectName as vcrProj19_41_2_,
usergroup1_.oid as oid1_65_0_,
usergroup1_.archived as archived2_65_0_,
usergroup1_.archivedDateTime as archived3_65_0_,
usergroup1_.version as version4_65_0_,
usergroup1_.description as descript5_65_0_,
usergroup1_.name as name6_65_0_,
usergroup1_.type as type7_65_0_,
usergroup_2_.oid as oid1_58_1_,
usergroup_2_.archived as archived2_58_1_,
usergroup_2_.archivedDateTime as archived3_58_1_,
usergroup_2_.version as version4_58_1_,
usergroup_2_.scmUserOid as scmUserO6_58_1_,
usergroup_2_.userGroupOid as userGrou5_58_1_
from
.PROJECT this_
inner join
.USERGROUP usergroup1_
on this_.userGroupOid=usergroup1_.oid
inner join
.SCMUSER_USERGROUP usergroup_2_
on usergroup1_.oid=usergroup_2_.userGroupOid
where
(
(
usergroup_2_.scmUserOid=3
)
or (
this_.userGroupOid is null
)
)
問題在於,這只是返回具有相關UserGroup的項目,因為它是內部聯接。 這意味着沒有定義用戶組就無法獲得項目。
我發現解決此問題的方法是對兩個聯接都使用左外部聯接,但是我不知道該如何進行工作。 問題是我不能碰到addX方法,因為這些方法在服務類中的任何地方都絕對使用。
如何調整此代碼,以使生成的SQL使用左外部聯接而不是內部聯接?
經過更多研究后,我發現解決此問題的正確方法是在createAlias()中添加joinType:
detachedCriteria.createAlias(currentAssociationPath, currentAliasName, JoinType.LEFT_OUTER_JOIN);
但是,這是一個相當復雜的修復程序,因為目前沒有使用joinType參數的地方,這意味着我需要與更多高級開發人員進一步討論。 現在,我將使用單獨的查詢來檢索顯式和隱式訪問權限,並在Java中將它們連接在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.