java - 使用 JPL 在 Java 中的 BDB 中存储子类

标签 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

}

类(class)

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

最佳答案

好吧,我明白了:

子类必须是

@Persistent
public class BdbIcd10DiagnosisCode extends AbstractBdbIcdCode implements ICD10DiagnosisCode{
}

关于java - 使用 JPL 在 Java 中的 BDB 中存储子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828715/

相关文章:

Java Berkeley DB DPL - 读取操作峰值

使用字母数字字符串进行验证的 Java 程序没有给我所需的输出

java - 优化 BerkeleyDB JE 数据库

Java:应用程序启动方法中出现异常 java.lang.reflect.InitationTargetException

database - Berkeley DB 中的多线程应用程序

c++ - Berkeley DB 无法使用不同的 Dbt 初始化设置值

berkeley-db - 如何打开本地比特币数据库

java - 如何使用 JPA/Hibernate 在两个相等的表之间复制多行?

java - 什么方法声明接受有界和无界的多级泛型?