0

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

才不是。

我该怎么做?

4

1 回答 1

0

感谢 DataNucleus 用户的评论和官方网站上的文档,这就是我所缺少的。

ElementOne 需要一个PrimaryKey 类,以便我们可以使用构造函数在主类的 PrimaryKey 中接受字符串参数。

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 网站的示例使用GWT中未实现的StringTokenizer,请改用 String.split() 。此外,java doc 指出:

StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它。建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包。

于 2010-12-16T10:00:52.297 回答