Getting object from cache?

From: McDaniel, Joe R. (joe.mcdanie..gc.com)
Date: Mon Mar 06 2006 - 15:53:29 EST

  • Next message: Andrus Adamchik: "Re: Getting object from cache?"

    I have not used Cayenne for about a year and thought I understood how it
    works. I want to get a value from the database if it exists, otherwise
    add it. If I add an object, I assumed it would be retrieved from the
    cache on the next iteration needing that same value, but that only
    happens if I have done a commit. Must I always do a commit or is there
    some other magic to get what I want working to do the "right thing?"

    Best,

    Joe

        Expression qual = ExpressionFactory.matchExp("lexical", value);
      
        SelectQuery query = new SelectQuery(Symbols.class, qual);

        // configure query to use local cache (supposedly this was the
    "magic")
        query.setName("FindSymbol");
        query.setCachePolicy(GenericSelectQuery.LOCAL_CACHE);
        
        List values = ctxt.performQuery(query);
        
        Symbols symbol = null;
        Long retval = null;
        if(values == null || values.size() == 0)
        {
          // value does not exist, add new value
          Symbols newSymbol =
    (Symbols)ctxt.createAndRegisterNewObject(Symbols.class);
          newSymbol.setLexical(value);
          retval = new Long(pk++);
          newSymbol.setHash(retval);
          newSymbol.setDatatype(new Integer(0));
          newSymbol.setLanguage(new Integer(0));
        } else
        {
          symbol = (Symbols)values.get(0);
          retval = symbol.getHash();
          hits++;
        }
        return retval;



    This archive was generated by hypermail 2.0.0 : Mon Mar 06 2006 - 15:53:31 EST