[英]DataNucleus Enhancer fails on LongIdentity. (datanucleus-maven-plugin 5.0.2)
我試圖創建復合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());
}
...
產品.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。
基於討論和對決定使用哪種身份的案例的試驗,我發現通過在中間類中添加一個字段作為其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<>();
...
產品.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.