为什么继承深度在 GAE 上的 Datanucleus JDO 中很重要?
如果我有一层继承,则 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并且您的主键现在不在(持久)继承树的根类中,因此 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.
您还应该将以下声明移至 Human 类:
You should also move the following declaration to the Human class: