我有两个实体:
第一个是:
public class WordEntity {
@PrimaryKey
private String content;
private int wordId;
}
第二个是:
public class LexiconEntity {
@SecondaryKey(relate = Relationship.ONE_TO_ONE, relatedEntity = WordEntity.class)// it does not work
private int wordId;
private int numDocs;
}
我希望LexiconEntity
的wordId
是WordEntity
的外键。我该怎么做?
答案很晚,但。。。首先,看起来wordId将是WordEntity的一个更自然的PK。LexiconEntity还应该定义PrimaryKey。WordEntity应该定义SecondaryKey,它引用LexiconEntity或"指定与此实体相关的实体"。
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;
}
因此数据为:
LexiconEntity:lexId-----100101102WordEntity:wordId lexId-----------11002 1013 102
由于关系是one_to_one,所以次关键字对于定义它的对象是唯一的。因此,在这种情况下,lexId在WordEntity中是唯一的,因此您不能有:
WordEntity:wordId lexId-----------11002 1013 100--插入时出现异常,因为它是一个dup
请参阅http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/dplindexcreate.html#dplsecondaryidxdeclhttp://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/persist/model/SecondaryKey.html#relatedEntity()