我有两个实体:
第一个是:
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;
}
我希望wordId
的{{1}}成为LexiconEntity
的外键。我怎么能这样做?
答案 0 :(得分:0)
迟到的答案,但是......首先,看起来wordId对于WordEntity来说将是更自然的PK。 LexiconEntity还应该定义一个PrimaryKey。 WordEntity应该定义引用LexiconEntity的SecondaryKey或“指定与该实体相关的实体”。
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 ----- 100 101 102 WordEntity: wordId lexId ------ ----- 1 100 2 101 3 102
由于关系是one_to_one,因此辅助键对于定义它的对象是唯一的。所以在这种情况下,lexId在WordEntity中是唯一的,所以你不能拥有:
WordEntity: wordId lexId ------ ----- 1 100 2 101 3 100 -- Exception on insert since it is a dup
参见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()