Java 伯克利数据库中的外键
我有两个实体: 第一个是:Java 伯克利数据库中的外键,java,database,foreign-keys,berkeley-db,berkeley-db-je,Java,Database,Foreign Keys,Berkeley Db,Berkeley Db Je,我有两个实体: 第一个是: 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 wor
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。词典统一性还应该定义一个PrimaryKey。WordEntity应该定义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
因为关系是一对一的,所以次键对于定义它的对象是唯一的。因此,在本例中,lexId在WordEntity中是唯一的,因此您不能有:
WordEntity:
wordId lexId
------ -----
1 100
2 101
3 100 -- Exception on insert since it is a dup
看
词典统一性不也应该有主键吗?编译器是否产生任何错误?