0

datanucleus で複合キーを使用してクラスをマップしようとしています。主キーは 2 つの外部キーで構成されており、これらの外部クラスを 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 Webサイトの例では、GWTに実装されていないStringTokenizerを使用しています。代わりに、String.split()を使用してください。さらに、javadocには次のように記載されています。

StringTokenizerは、互換性の理由で保持されているレガシークラスですが、新しいコードでは使用しないでください。この機能を求める人は、代わりにStringのsplitメソッドまたはjava.util.regexパッケージを使用することをお勧めします。

于 2010-12-16T10:00:52.297 に答える