Datanucleus fetchgroup 复合键

发布于 2024-10-06 17:48:40 字数 793 浏览 0 评论 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;

不能。

我该怎么办?

I am trying to map a class with composite key in datanucleus. The primary key is composed of two foreign keys and I can't seem to be able to include these foreign classes in the fetchgroup:

Using annotations :

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

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

works

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

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

works

but

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

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

does not.

How am I meant to do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-10-13 17:48:40

感谢 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 网站的示例使用 StringTokenizer ,即 未在 GWT 中实现,请使用 String.split() 代替。此外,java 文档指出:

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

Thanks to comments from DataNucleus user and documentation from the official website here is what I was missing.

ElementOne needs a PrimaryKey class so that we can use a constructor accepting a string argument in the main class' PrimaryKey.

ElementOne PrimaryKey class:

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;
        }

        //...
    }

Main class with its PrimaryKey class:

 @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: Examples from DataNucleus website use StringTokenizer which is not implemented in GWT, use String.split() instead. Moreover the java doc states that:

StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文