使用JPL在Java中使用BDB存储子类

时间:2014-09-13 23:26:53

标签: java berkeley-db berkeley-db-je

我试图坚持使用JPL,将我的对象变成一个简单的BDB。问题是,如果我尝试在主索引中存储子类,我会收到错误(下面)。

子类在数据方面没有任何不同,但它们在功能上是不同的,我希望子类是编组的实例类型。如果我必须,那么密钥可以包含有问题的类,我可以自己实例化

也许每个子类需要一个索引?但这似乎并不正确,肯定会使查找过程复杂化。

示例:

@Test
public void testStoreEntity() throws Exception {
    Random r = new Random();
    File directory = new File("C:\\Users\\chribong\\development\\code\\edifecs\\engineering\\platform\\icd-code-factory\\src\\test\\data\\" + r.nextInt(1000));
    directory.mkdirs();
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    Environment env = new Environment(directory, envConfig);

    StoreConfig storeConfig = new StoreConfig();
    storeConfig.setAllowCreate(true);
    storeConfig.setTransactional(true);
    EntityStore store = new EntityStore(env, "PersonStore", storeConfig);

    PrimaryIndex<Key,AbstractBdbIcdCode> index = store.getPrimaryIndex(Key.class,AbstractBdbIcdCode.class);

    index.put(new AbstractBdbIcdCode(10,"foo", ICDCode.Type.DIAGNOSIS,".*"));
    index.put(new BdbIcd10DiagnosisCode("A000")); // error here
    // java.lang.IllegalArgumentException: Class could not be loaded or is not persistent

}

@Entity
public class AbstractBdbIcdCode implements ICDCode {

    @PrimaryKey
    private Key pk;

    protected String description;

    protected Set<String> concepts = new TreeSet<>();
    protected Set<String> antiConcepts = new TreeSet<>();
    protected Set<String> similarConcepts = new TreeSet<>();
}

@Persistent
public class Key {

    @KeyField(1)
    private ICDCode.Type type;

    @KeyField(2)
    private int version;
    @KeyField(3)
    private String code;

    public Key() {
    }
}

public class BdbIcd10DiagnosisCode extends AbstractBdbIcdCode implements ICD10DiagnosisCode{


    private transient String category;

    private transient  String etiology;

    private transient  String location;

    private String laterality;
    protected String extension;
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了:

子类必须是

@Persistent
public class BdbIcd10DiagnosisCode extends AbstractBdbIcdCode implements ICD10DiagnosisCode{
}