为什么继承深度在 GAE 上的 Datanucleus JDO 中很重要?

发布于 2024-12-01 16:54:13 字数 1550 浏览 1 评论 0原文

如果我有一层继承,则 App Engine 中的所有内容都会按预期保留:

Worker.java

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Worker {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String department;
}

Employee.java

// ... imports ...

@PersistenceCapable
public class Employee extends Worker {
    @Persistent
    private int salary;

}

Intern.java

import java.util.Date;
// ... imports ...

@PersistenceCapable
public class Intern extends Worker {
    @Persistent
    private Date internshipEndDate;
}

但是,如果我添加一层额外的继承,则最高级别子类中的字段不会保留:

Human .java

@PersistenceCapable
public abstract class Human {

    @Persistent
    private String name;
}

Worker.java

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Worker extends Human {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String department;
}

为什么可以扩展抽象类,但不能让该抽象类扩展更高级别的类?

If I have one level of inheritance, everything is persisted as expected in App Engine:

Worker.java

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Worker {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String department;
}

Employee.java

// ... imports ...

@PersistenceCapable
public class Employee extends Worker {
    @Persistent
    private int salary;

}

Intern.java

import java.util.Date;
// ... imports ...

@PersistenceCapable
public class Intern extends Worker {
    @Persistent
    private Date internshipEndDate;
}

However, if I add one additional layer of inheritance, the fields in the highest level subclass are not persisted:

Human.java

@PersistenceCapable
public abstract class Human {

    @Persistent
    private String name;
}

Worker.java

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Worker extends Human {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String department;
}

Why is it ok to extend an abstract class but not ok to have that abstract class extend a higher level class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2024-12-08 16:54:13

并且您的主键现在不在(持久)继承树的根类中,因此 JDO 和 JPA 规范无效。

And your Primary key is now not in the root class of the (persistable) inheritance tree, hence invalid by the JDO and JPA specs.

倒数 2024-12-08 16:54:13

您还应该将以下声明移至 Human 类:

@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)

You should also move the following declaration to the Human class:

@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文