2010-12-06 58 views
0

我想映射與datanucleus中的複合鍵的類。主鍵是由兩個外鍵,我似乎無法能夠包括這些外資類的fetchgroup:Datanucleus fetchgroup複合鍵

使用註釋:

@PrimaryKey 
@Column(name = idElementOne, allowsNull = "false") 
private Long idElementOne; 

@PrimaryKey 
@Column(name = "idElementTwo", allowsNull = "false"); 
private Long idElementTwo; 

工作

@PrimaryKey 
@Column(name = idElementOne, allowsNull = "false"); 
private ElementOne elementOne; 

@Column(name = "idElementTwo", allowsNull = "false"); 
private Long idElementTwo; 

作品

@PrimaryKey 
@Column(name = idElementOne, allowsNull = "false") 
private ElementOne elementOne; 

@PrimaryKey 
@Column(name = "idElementTwo", allowsNull = "false"); 
private Long idElementTwo; 

沒有。

我該怎麼做?

+0

您還沒有提及任何獲取組元數據或使用獲取組API。所以不知道爲什麼你認爲某個特定的抓取組中有什麼東西 – DataNucleus 2010-12-08 07:47:32

回答

0

感謝來自DataNucleus用戶和官方網站文檔的評論,這裏是我所缺少的。

ElementOne需要一個PrimaryKey class以便我們可以在主類PrimaryKey中使用use a constructor accepting a string argument

ElementOne的PrimaryKey類:

public static class PK implements Serializable 
{ 
     public Long idElementOne; 

     public PK() 
     { 
     } 

     public PK(String s) 
     { 
      this.idElementOne = Long.valueOf(s); 
     } 

     public String toString() 
     { 
      return "" + idElementOne; 
     } 

     //... 
    } 

主要類及其PrimaryKey的類:

@PersistenceCapable(objectIdClass=PK.class) 
public class MainClass{ 

@PrimaryKey 
@Column(name = idElementOne, allowsNull = "false") 
private ElementOne elementOne; 

@PrimaryKey 
@Column(name = "idElementTwo", allowsNull = "false"); 
private Long idElementTwo; 

//... 

public static class PK implements Serializable 
{ 
     public Long idElementTwo; // Same name as real field in the main class 
     public ElementOne.PK elementOne; // Same name as the real field in the main class 

     public PK() 
     { 
     } 

     public PK(String s) 
     { 
      String[] constructorParam = s.split("::"); 
      this.idElementTwo= Long.parseLong(constructorParam[1]); 
      this.personne = new Personne.PK(constructorParam[2]); 

     } 

     public String toString() 
     { 
      return "" + idElementTwo+ "::" + this.personne.toString(); 
     } 

     //... 
    } 
} 

PS:從網站DataNucleus將使用StringTokenizernot implemented in GWT實施例中,使用String.split()代替。此外,該Java文檔指出:

的StringTokenizer是一個遺留類 保持兼容性的原因 雖然它的使用是在新 代碼氣餒。建議任何尋求此功能的 都使用String的 拆分方法或代替使用java.util.regex包的 。