【问题标题】:Hibernate HQL list attributes?Hibernate HQL 列表属性?
【发布时间】:2014-01-29 07:17:12
【问题描述】:

我有一个类似这样的小查询:

String sqlStr = "select count(*) from MyTable n where primaryKey.userId = :userId and primaryKey.userType = :userType"

String[] paramNames = {"userId", "userType" };
Object[] paramValues = {userId, userType};

List myObjects = (List) hibernateTemplate.findByNamedQueryAndNamedParam("objectsToDeleteForUser", paramNames, paramValues);

这很好用。我可以使用用户 ID 和用户类型拨打电话并获取一条或多条匹配记录。

我现在想做的是拥有一个用户 ID 和用户类型的列表并执行调用,但不必遍历列表而是进行一次调用。所以基本上当userid(0) and usertype(0), userid(1) and usertype(1) ....................

是否可以通过 HQL 中的一次调用来执行此操作。类似于 IN 子句 (X)。

谢谢

【问题讨论】:

  • AFAIK,这不受 HQL 支持。另一个不使用复合主键的好理由。使用自动生成的单列主键,一切都会变得容易得多。

标签: java hibernate hql


【解决方案1】:

这样的事情是可能的:

hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
hibernateSession.beginTransaction();

List<Object> userId = new ArrayList<Object>();
List<Object> userType= new ArrayList<Object>();

Query q1 = hibernateSession.createQuery("select count(*) from MyTable n where primaryKey.userId IN (:userId) and primaryKey.userType IN (:userType)");

q1.setParameterList("userId", userId);
q1.setparameterList("userType" , userType);

List myObjects  = q1.list();

hibernateSession.close();

这应该对你有用

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 2016-04-21
    • 1970-01-01
    • 2011-05-18
    • 2011-06-16
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多