1

I have two entities:

The first one is:

public class WordEntity {
    @PrimaryKey
    private String content;

    private int wordId;
}

The second one is:

public class LexiconEntity {
    @SecondaryKey(relate = Relationship.ONE_TO_ONE, relatedEntity = WordEntity.class)// it does not work
    private int wordId;

    private int numDocs;
}

I want to wordId of the LexiconEntity be the foreign key of that of WordEntity. How can I do this?

CSnerd
  • 2,129
  • 8
  • 22
  • 45

1 Answers1

0

Late answer, but... First it looks like the wordId would be a more natural PK for the WordEntity. The LexiconEntity should also define a PrimaryKey. The WordEntity should define the SecondaryKey which refers to the LexiconEntity or "Specifies the entity to which this entity is related".

public class WordEntity {
    @PrimaryKey
    private int wordId;
    private String content;
    @SecondaryKey(relate = Relationship.ONE_TO_ONE, relatedEntity = LexiconEntity.class)
    private int lexId;
}

public class LexiconEntity {
    @PrimaryKey
    private int lexId;
    private int numDocs;
}

Therefore data would be:

LexiconEntity:
lexId
-----
100
101
102

WordEntity:        
wordId  lexId
------  -----
1       100
2       101
3       102

Since the relationship is one_to_one, the secondary key is unique to the object defining it. So in this case, lexId is unique in the WordEntity, so you cannot have:

WordEntity:        
wordId  lexId
------  -----
1       100
2       101
3       100 -- Exception on insert since it is a dup

See http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/dplindexcreate.html#dplsecondaryidxdecl http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/persist/model/SecondaryKey.html#relatedEntity()

pjscore10
  • 91
  • 4