DataNucleus Enhancer在LongIdentity上失败。 (datanucleus-maven-plugin 5.0.2)

时间:2018-02-09 11:01:11

标签: java jdo datanucleus

我正在尝试按照记录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<>();
  ...

Company.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。

1 个答案:

答案 0 :(得分:0)

根据讨论和我对这个决定使用哪个身份的案例的审判,我发现通过向中间类添加一个字段为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; 

...

Company.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<>();
...

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