Re: object with toMany.size() == 0?

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Sat Oct 07 2006 - 14:08:19 EDT

  • Next message: Andrus Adamchik: "Re: [announce] Click Framework 1.0 is now available"

    There are a few solutions until we address this case in a more direct
    way (which I am ashamed to say we do not now) -

    1. For in-memory filtering, you can create a cover method "getSize()"
    to work around JavaBean naming.

    2. For DB query, if you don't have a lot of objects in those tables,
    you can use a modified advice from here:

    http://cwiki.apache.org/CAY/not-in-to-many-faq.html

      SelectQuery excludedQuery = new SelectQuery(House.class);
      // add joint prefetch that will result in an inner join to ensure
    houses without residents are excluded
      excludedQuery.addPrefetch("residents").setSemantics
    (PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);

      List excluded = ctxt.performQuery(excludedQuery);
      List all = ctxt.performQuery(new SelectQuery(House.class));
      all.removeAll(excluded);

    3. The most efficient approach is SQLTemplate that fetches the right
    objects on the first attempt.

    4. My favorite approach - fetch residents instead of houses, and then
    build a set of distinct houses by iterating via the list of residents
    in memory. Again the efficiency depends on how big is the residents
    list.

    Andrus

    On Oct 6, 2006, at 10:51 PM, Arturo Pérez wrote:

    > Hi all,
    >
    > I thought I saw this somewhere on the wiki but I can't find it.
    >
    > I have an object with a toMany relation (e.g. house.getResidents()).
    >
    > How do I write an Expression to find all the houses with no residents?
    >
    > I know this doesn't work:
    > Expression exp = ExpressionFactory.matchExp("residents", null);
    >
    > and this doesn't work because the size() method doesn't follow bean
    > convention:
    >
    > Expression exp = ExpressionFactory.matchExp("residents.size", new
    > Integer(0));
    >
    >
    > I would be much obliged...
    > -arturo
    >
    >



    This archive was generated by hypermail 2.0.0 : Sat Oct 07 2006 - 14:08:25 EDT