使用JDO / Datanucleus保持接口

时间:2011-10-03 13:50:42

标签: jdo datanucleus

我有以下课程:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class TclRequest implements Comparable<TclRequest> {
    @PrimaryKey
    private String id;

    @Persistent(types = { DNSTestData.class, POP3TestData.class, PPPoETestData.class, RADIUSTestData.class }, defaultFetchGroup = "true")
    @Columns({ @Column(name = "dnstestdata_fk"), @Column(name = "pop3testdata_fk"), @Column(name = "pppoetestdata_fk"), @Column(name = "radiustestdata_fk") })
    private TestData testData;

    public String getId() {
        return id;
    }

    public TestData getTestData() {
        return testData;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setTestData(TestData testData) {
        this.testData = testData;
    }
}

TestData界面如下所示:

@PersistenceCapable(detachable = "true")
    public interface TestData {
    @PrimaryKey
    public String getId();

    public void setId(String id);
}

这是由许多类别实施的,包括这一个:

@PersistenceCapable(detachable = "true")
public class RADIUSTestData implements TestData {
    @PrimaryKey
    private String id;
    private String password;
    private String username;

    public RADIUSTestData() {
    }

    public RADIUSTestData(String password, String username) {
        super();
        this.password = password;
        this.username = username;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }
}

当我尝试持久化TclRequest类时,在构建它之后并使用RADIUSTestData:

//'o' is the constructed TclRequest object.
PersistenceManager pm = null;
Transaction t = null;
try {
    pm = getPM();
    t = pm.currentTransaction();
    t.begin();
    pm.makePersistent(o);
    t.commit();
} catch (Exception e) {
    e.printStackTrace();
    if (t != null && t.isActive()) {
        t.rollback();
    }
} finally {
    closePM(pm);
}

接口字段未保留。并且表中没有创建列!我启用了调试模式并找到了两个引人注目的东西:

1) -Class com.skycomm.cth.beans.ixload.radius.TestData指定使用“application identity”但未指定“objectid-class”。恢复为javax.jdo.identity.StringIdentity

2) - 在PC字段“com.skycomm.cth.beans.TclRequest.testData”上执行可达性 - 在字段“com.skycomm.cth.beans.TclRequest.testData”中找不到用于PC对象的StateManager“ - 忽略可达性

这意味着什么? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我已经想出了怎么做。它的可扩展性不是很高,但现在可以使用。

这些是接口成员变量的注释。请注意,扩展值中声明的类型,列和类名称的顺序非常重要:

@Persistent(types = { RADIUSTestData.class, POP3TestData.class, PPPoETestData.class, DNSTestData.class }, defaultFetchGroup = "true")
@Columns({ @Column(name = "radiustestdata_fk"), @Column(name = "pop3testdata_fk"), @Column(name = "pppoetestdata_fk"),
@Column(name = "dnstestdata_fk") })
@Extension(vendorName = "datanucleus", key = "implementation-classes", value = "com.skycomm.cth.tcl.beans.radius.RADIUSTestData, com.skycomm.cth.tcl.beans.pop3.POP3TestData, com.skycomm.cth.tcl.beans.pppoe.PPPoETestData, com.skycomm.cth.tcl.beans.dns.DNSTestData")

实现其中一个接口的示例类(Just it's“header”):

@PersistenceCapable(detachable = "true")
public class RADIUSTestData implements TestData {

所以这里非常正常。