Datanucleus fetchgroup复合键

时间:2010-12-06 15:07:41

标签: datanucleus composite-key

我正在尝试使用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;

没有。

我该怎么做?

1 个答案:

答案 0 :(得分: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网站使用StringTokenizer not implemented in GWT的示例,请改用String.split()。此外,java doc声明:

  

StringTokenizer是一个遗留类   因兼容性原因而保留   虽然在新的情况下不鼓励使用它   码。任何人都可以推荐   寻求此功能使用   分裂方法的String或   而是java.util.regex包。