English 中文(简体)
使用 API 标准进行的复杂冬眠查询
原标题:Complex hibernate query using criteria API

我试图用标准 API 来写一个复杂的查询。 我需要一些帮助。

我有3个类别,资产、资产元件和任务。任务类别与资产元件有1到M关系,资产元件与资产有1到M关系。我需要找到所有与资产有关的任务(任务有一个组成部分,组成部分知道与之相关的资产)的资产(应该是独特的清单)。

这是我至今为止拥有的

public List<Asset> retrieveTask(Project project, boolean assigned) {

  DetachedCriteria subquery = DetachedCriteria.forClass(Task.class);

  subquery.add(Restrictions.eq("project", project));
  if (assigned) {
     subquery.add(Restrictions.isNotNull("assignedTo"));
  } else {
     subquery.add(Restrictions.isNull("assignedTo"));
  }

  subquery = subquery.createCriteria("component");
  subquery.add(Restrictions.isNotNull("asset"));

  Criteria criteria = super.createCriteria();
  criteria.add(Subqueries.in("id", subquery));

  return criteria.list();
}

解密查询应该返回所有被分配到资产并且是赠予项目一部分的任务。 现在我需要看看所有这些任务的独特资产是什么。

谢谢你帮我这个忙

干杯 干杯

最佳回答

在黑暗中打一针, 但如何这如何为你工作:

public List<Asset> retrieveTask(Project project, boolean assigned) {

    DetachedCriteria subquery = DetachedCriteria.forClass(Task.class);

    subquery.add(Restrictions.eq("project", project));
    DetachedCriteria assetCriteria = subquery
        .createCriteria("component")
        .createCriteria("asset").setProjection(Projections.groupProperty("id"));

    Criteria criteria = super.createCriteria();
    if(assigned) {
        criteria.add(Subqueries.propertyIn("id", subquery));
    }
    else {
        criteria.add(Subqueries.propertyNotIn("id", subquery));
    }
    return criteria.list();
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签