我有两个实体:

第一个是:

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;
}


我想将wordIdLexiconEntity用作WordEntity的外键。我怎样才能做到这一点?

最佳答案

答案较晚,但是...首先,对于WordEntity来说,wordId似乎是更自然的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;
}


因此数据将是:

词汇实体:
lexId
-----
100
101
102

字实体:
wordId lexId
------ -----
1100
2 101
3 102


由于关系是one_to_one,因此辅助键对于定义它的对象是唯一的。因此,在这种情况下,lexId在WordEntity中是唯一的,因此您不能拥有:

字实体:
wordId lexId
------ -----
1100
2 101
3 100-由于是双重处理,因此插入时发生异常


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()

08-27 10:15