由於它是不可能只使用Long ID我試圖使用生成的字符串鍵。我有三個類User
,Topic
,Comments
與User
- 1:n - Topic
- 1:n - Comments
。GAE數據存儲與JPA生成字符串鍵
類註釋:
@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;
現在,當我牛逼rying保存新的用戶,將出現以下異常
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?如果是,我的代碼有什麼問題?
謝謝
不在GAE上,您不 – DataNucleus