1 Reply Latest reply: Jul 4, 2012 11:30 AM by Greybird-Oracle RSS

    Composite keys

    user13358531
      I'm doing a class with a composite @PrimaryKey ex:

      @Persistent
      public class NewsKey {
           @KeyField(1) Long datetime;
           @KeyField(2) String ccy;
      }

      @Entity
      public class News {
           @PrimaryKey
           private NewsKey newsKey;
      }

      I have a PrimaryIndex for this key:

      PrimaryIndex<NewsKey, News> newsByKey = mystore.getPrimaryIndex(NewsKey.class, News.class);

      When I do the search by this composite key:

      long fromTime = ; // some time number
      long totime = ; // some time number
      NewsKey fromKey = new NewsKey(fromTime, ccy);
      NewsKey toKey = new NewsKey(toTime, ccy);
      boolean fromInclusive = true;
      boolean toInclusive = true;
      newsByKey.entities(fromKey, fromInclusive, toKey, toInclusive);

      The entities returned doesn't take into account the ccy String value.

      But when I invert the order of key fields e.g.

      @Persistent
      public class NewsKey {
           @KeyField(1) String ccy;
           @KeyField(2) Long datetime;
      }

      The search returns the correct result doing the search by ccy and by datetime.

      Does the order or @KeyField influence in the results?
        • 1. Re: Composite keys
          Greybird-Oracle
          Please see the example and explanation at the top of this page:
          http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/persist/model/KeyField.html

          Does this clarify things?
          --mark