0

長いIDのみを使用することはできないため、生成された文字列キーを使用しようとしています。私には3つのクラスがありUserますTopic。- 1:n --- 1:n- 。CommentsUserTopicComments

クラスコメント:

@Entity
public class Comment implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
    @ManyToOne
    private User author;
    @ManyToOne
    private Topic topic;

クラスユーザー:

@Entity
public class User implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;

    @Unique
    private String username;

クラストピック:

@Entity
public class Topic implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key;
    @ManyToOne(cascade = CascadeType.ALL)
    private User author;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Comment> comments;

新しいユーザーを保存しようとすると、次の例外が発生します

Invalid primary key for User.  Cannot have a null primary key field if the field is unencoded and of type String.  Please provide a value or, if you want the datastore to generate an id on your behalf, change the type of the field to Long. 

KeyFactoryを手動で使用せずに文字列IDを生成させることは可能ですか?はいの場合、私のコードはどうなっていますか?

ありがとう

4

2 に答える 2

1

IIRC IDENTITY 戦略は、数値 (またはキー) ID を生成することです。JDO を使用している場合は、自動生成された UUID スタイルの ID を使用できます。https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata#Keysを参照してください

于 2012-10-26T07:30:28.313 に答える
0

私は使用しますTableGeneratorwantどんなスタイルでも重宝します。GroupなどのidGRP0000001を取得したい場合でも言いましょうGRP0000500。エンティティでは、フィールド インジェクションではなく、プロパティ インジェクションを使用する必要があります。これは、セッター ID メソッドに基づいています。生成された ID が生成によって 201 である場合EntityManager、エンティティ マネージャーはsetId()セッター インジェクションを呼び出します。その場合、id は になりますGRP0000201

私の例:

@Entity
@TableGenerator(name = "GROUP_GEN", table = "ID_GEN", pkColumnName = "GEN_NAME", 
                valueColumnName = "GEN_VAL", pkColumnValue = "GROUP_GEN", allocationSize = 1)
@Access(value = AccessType.FIELD)
public class Group implements Serializable {
    @Transient
    private String id;
    private String name;
    //name getter and setter
    public void setId(String id) {
        if(id != null) {
            this.id = Utils.formatId(id, "GRP", 10);    
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "GROUP_GEN")
    @Access(value = AccessType.PROPERTY)
    public String getId() {
        return id;
    }
}

Utils.java

public class Utils {
    /**
     * E.g.
     * Input: id=523, prefix="AAA", maxLength=15
     * Output: AAA000000000523
     */
    public static String formatId(String id, String prefix, int maxLength) {
        if (!id.startsWith(prefix)) {
            int length = id.length() + prefix.length();
            for (; (maxLength - length) > 0; length++) {
                id = '0' + id;
            }
            id = prefix + id;
        }
        return id;
    }
}
于 2012-10-25T10:05:51.943 に答える