Recall object by it's PK? (e.g. w/Lucene)

From: Craig Jones (crai..ankardsaweigh.com)
Date: Mon Apr 19 2004 - 14:45:51 EDT

  • Next message: Mike Kienenberger: "Re: Recall object by it's PK? (e.g. w/Lucene)"

    Hi,

    I need help figuring out how to recall a data object given the PK.

    I want to use Lucene to do full-text searching on my data. My data
    object includes an auto-generated PK, a description field, and some
    other fields. I figure I'll have Lucene index the description and store
    the PK value to give back to me when I do a search. I found in the
    Cayenne documentation how to have the data object return the PK as a
    read-only property, as follows, so I know how to tell Lucene what the PK
    is.

            /** Read-only access to PK */
            public Integer getId() {
               return (getObjectId() != null &&
    !getObjectId().isTemporary())
                                    ?
    (Integer)getObjectId().getIdSnapshot().get(AD_ID_PK_COLUMN)
                                    : null;
            }

    My question is, after Lucene does a search and gives me back a list of
    PK numbers, how do I get Cayenne to fetch back the object(s) that
    correspond to those values? I tried doing an IN expression as follows

          // (Lucene query returns something called a Hits object here)

               ArrayList idList = new ArrayList(hits.length());
            for (int i = 0; i < hits.length(); i++) {
                    idList.add(Integer.getInteger(hits.doc(i).get("Id")));
    }

            Expression qual = ExpressionFactory.inExp("getId", idList); //
    Tried with and w/o the "get"
            SelectQuery cayenneQuery = new SelectQuery(MyObject.class,
    qual);
          return ctxt.performQuery(cayenneQuery);

    But Cayenne can't resolve the expression (presumably because the PK
    property is not part of the mappings in the XML file?)

    org.objectstyle.cayenne.CayenneRuntimeException: [v.1.0.5 December 22
    2003] Global exception.
            <clip>
    Caused by: org.objectstyle.cayenne.exp.ExpressionException: [v.1.0.5
    December 22 2003] Can't resolve path component: [MyObject.getId].
            at
    org.objectstyle.cayenne.map.Entity$PathIterator.next(Entity.java:309)
            at
    org.objectstyle.cayenne.access.trans.QueryAssemblerHelper.appendObjPath(
    QueryAssemblerHelper.java:127)
            at
    org.objectstyle.cayenne.access.trans.QualifierTranslator.objectNode(Qual
    ifierTranslator.java:355)
            at
    org.objectstyle.cayenne.exp.ExpressionTraversal.traverseExpression(Expre
    ssionTraversal.java:90)
            at
    org.objectstyle.cayenne.exp.ExpressionTraversal.traverseExpression(Expre
    ssionTraversal.java:117)
            <clip>

    How do I tell Cayenne that the getId property is available for searching
    without mucking up the auto-PK generation?

    Thanks in Advance,

    // Craig
    Orange, CA

    P.S. I don't suppose that there is any tutorial information available on
    using Lucene with Cayenne that I missed somewhere? If not, I guess I
    get to be the one to write it, huh?



    This archive was generated by hypermail 2.0.0 : Mon Apr 19 2004 - 14:43:02 EDT