【问题标题】:DataNucleus Enhancer fails on LongIdentity. (datanucleus-maven-plugin 5.0.2)DataNucleus Enhancer 在 LongIdentity 上失败。 (datanucleus-maven-plugin 5.0.2)
【发布时间】:2018-02-09 11:01:10
【问题描述】:

我试图为 here 记录的多对多属性关系创建如下复合 PK:但当我使用 LongIdentity 时无法增强。现在的DataNucleus Enhancer不兼容吗?

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true",
    objectIdClass = CompanyProduct.PK.class)
public class CompanyProduct implements Serializable {

private static final long serialVersionUID = -7578808257727591074L;

@PrimaryKey
private Company company; // PK
@PrimaryKey
private Product product; // PK
...

public static class PK implements Serializable {

    private static final long serialVersionUID = -2090216333556951760L;
    public LongIdentity company; // Use same name as CompanyProduct field
    public LongIdentity product; // Use same name as CompanyProduct field

    public PK() {
    }

    public PK(String s) {
        StringTokenizer st = new StringTokenizer(s, "::");
        this.company = new LongIdentity(Company.class, st.nextToken());//
        this.product = new LongIdentity(Product.class, st.nextToken());//
    }

    @Override
    public String toString() {
        return (company.toString() + "::" + product.toString());
    } 
...

Product.java

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true")
public class Product implements Serializable {

private static final long serialVersionUID = 8269335445554701873L;


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
long id;
@Persistent(mappedBy = "product")
private Set<CompanyProduct> companyProducts = new HashSet<>();
  ...

公司.java

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true")
public class Company implements Serializable {

private static final long serialVersionUID = 6364869685797117033L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
//private 
long id;
@Persistent(mappedBy = "company")
private Set<CompanyProduct> companyProducts = new HashSet<>();

我做错了什么? datanucleus-maven-plugin 版本是 5.0.2,datanucleus-core 版本是 5.1.6。

【问题讨论】:

  • 不管您的问题是什么,您为什么要引用不受支持的旧版本的文档? (无论如何你似乎都没有使用)
  • @Billy 感谢您指出这一点。当前的文档和版本在哪里?
  • 好吧,当我想要他们的文档时,我会转到他们的主页并导航到我正在使用的版本;不知道为什么很难找到。 datanucleus.org:15080/products/accessplatform_5_1/jdo/…
  • 我看过文档。对于像我这样的懒人,我必须像文档中那样为每个班级创建PK吗?有没有办法使用“SingleFieldIdentity”内置PK?因为如果我使用public Company.PK company; 这意味着我必须为它创建 PK 类而不是内置的 PK 类。
  • "SingleFieldIdentity" 不被 DataNucleus 使用,除此之外,您将代码绑定到一些内部 JDO 类。只要您永远不需要引用它,并且通过在您的类中对其进行硬编码,您就可以引用它,“内置”类就很好。显然,您可以贡献不需要的处理

标签: java jdo datanucleus


【解决方案1】:

基于对决定使用哪个身份的这个案例的讨论和试验,我发现继续使用Application identity 很方便,方法是向中间类添加一个字段作为它的Application identity(生成值)。有了这些,我不需要手动编写 PK 类。因此,我的代码如下;

CompanyProduct.java (many_to_many_attributed)

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true")
public class CompanyProduct implements Serializable {

private static final long serialVersionUID = -7578808257727591074L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
long id;
@Persistent
private Company company;
@Persistent
private Product product; 

...

公司.java

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true")
public class Company implements Serializable {

private static final long serialVersionUID = 6364869685797117033L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
long id;
@Persistent(mappedBy = "company")
private Set<CompanyProduct> companyProducts = new HashSet<>();

...

Product.java

@PersistenceCapable(identityType = IdentityType.APPLICATION, cacheable = "false", detachable = "true")
public class Product implements Serializable {

private static final long serialVersionUID = 8269335445554701873L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
long id;
@Persistent(mappedBy = "product")
private Set<CompanyProduct> companyProducts = new HashSet<>();
...

这是我现在已经解决的问题,除非有人建议我不要使用“应用程序身份”。感谢@Billy 和@DN1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    相关资源
    最近更新 更多