我正在尝试为名为 Item 的 JDO 实体创建 PK 类。使用 JPA 非常简单,但现在我正在练习 JDO。我正在使用注释配置,这就是这两个类的样子:
@PersistenceCapable(table="ITEM",identityType = IdentityType.APPLICATION,
objectIdClass = ItemPK.class,schema="mgr")
public class Item {
@PrimaryKey
@Persistent(column="code")
private long code; //WHY public?
@PrimaryKey
@Persistent(column="producer")
private String producer;
@PrimaryKey
@Embedded
private ItemPK id;
@Persistent(column="price")
private double price;
@Persistent(column="name")
private String name;
@Persistent(column="description")
private String description;
[... getters/setters...]
}
我希望 ItemPK 类用作具有两列(代码、生产者)的主键类。这就是类的样子:
@EmbeddedOnly
@PersistenceCapable(embeddedOnly="true",identityType=IdentityType.APPLICATION)
public class ItemPK implements Serializable{
@Persistent
@PrimaryKey
public long code;
@Persistent
@PrimaryKey
public String producer;
@Override
public String toString() {
return code+"_"+producer;
}
@Override
public int hashCode() {
[...Eclipse autogenerated...]
}
@Override
public boolean equals(Object obj) {
[...Eclipse autogenerated...]
}
}
尝试运行代码后我得到了什么:
[...Caused by]
Nested Throwables StackTrace:
Class pl.edu.pw.mini.entity.jdo.Item has been specified with an object-id class pl.edu.pw.mini.entity.jdo.ItemPK which has a field jdoStateManager which isnt Serializable. All non static fields of an objectId class must be serializable.
org.datanucleus.metadata.InvalidPrimaryKeyException: Class pl.edu.pw.mini.entity.jdo.Item has been specified with an object-id class pl.edu.pw.mini.entity.jdo.ItemPK which has a field jdoStateManager which isnt Serializable. All non static fields of an objectId class must be serializable.
据我了解,增强器将 jdoStateManager 添加到 ItemPK,但它不可序列化。但由于 ItemPK 是嵌入的,它要么不应该获取 jdoStateManager,要么 JDO 应该知道 jdoStateManager 和常规字段之间的区别。我做错了什么来获取 2 列主键的嵌入类
我不知道如何让这件事发挥作用,任何人都可以帮助我,并告诉我我在这里做错了什么吗?
最佳答案
文档很好地定义了如何做到这一点 http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/compound_identity.html 并且它不涉及使用@Embedded
关于java - DataNucleus JDO 实现中的复合键(字段 jdoStateManager),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11766732/