1

私は2つのエンティティを持っています:

最初のものは次のとおりです。

public class WordEntity {
    @PrimaryKey
    private String content;

    private int wordId;
}

2 つ目は次のとおりです。

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

    private int numDocs;
}

wordIdの をのLexiconEntityの 外部キーにしたいWordEntity。これどうやってするの?

4

1 に答える 1

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

したがって、データは次のようになります。

レキシコンエンティティ:
lexID
-----
100
101
102

Wordエンティティ:        
単語 ID lexId
------ -----
1 100
2 101
3 102

関係は 1 対 1 であるため、2 次キーはそれを定義するオブジェクトに固有です。したがって、この場合、lexId は WordEntity 内で一意であるため、次のものを持つことはできません。

Wordエンティティ:        
単語 ID lexId
------ -----
1 100
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()

于 2014-10-30T15:26:33.210 に答える