gpt4 book ai didi

java - hibernate 标准 API 中的子查询

转载 作者:行者123 更新时间:2023-12-01 10:03:37 26 4
gpt4 key购买 nike

我正在尝试在 hibernate 条件中生成此查询:

 select count(*) from res_mapping where mop_id = ? and role_name = ? and 
mop_id not in(select mop_id from res_mapping_mod where mop_id = ? and role_name = ?);

这是我的方法:

public static boolean roleHasMenu(String roleName, String mopId) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
long count = 0;
try {
tx = session.beginTransaction();
DetachedCriteria subquery = DetachedCriteria.forClass(ResMappingMod.class);
subquery.add(Restrictions.eq("roleName", roleName));
subquery.add(Restrictions.eq("mopId", mopId));
subquery.setProjection(Projections.property("mopId"));
Criteria cr = session.createCriteria(ResMapping.class);
cr.add(Restrictions.eq("roleName", roleName));
cr.add(Restrictions.eq("mopId", mopId));
cr.add(Subqueries.notIn("mopId", subquery));
count = (Long) cr.setProjection(Projections.rowCount()).uniqueResult();
tx.commit();
} catch (Exception asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return count > 0;
}

我得到的是:

    select count(*) as y0_ from GLS.RES_MAPPING this_ where this_.ROLE_NAME=? 
and this_.MOP_ID=? and ? not in (select this_.MOP_ID as y0_ from
GLS.RES_MAPPING_MOD this_ where this_.ROLE_NAME=? and this_.MOP_ID=?)

Before not in 我有一个参数而不是字段。可能是什么问题?

最佳答案

试试这个

cr.add(Subqueries.propertyNotIn("id", subCriteria));

谢谢,阿米特·库马尔

关于java - hibernate 标准 API 中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623114/

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