由于不可能只使用 Long Id,我正在尝试使用生成的 String 键。我有三个 Classes User
, Topic
,Comments
带有User
- 1:n - Topic
- 1:n - Comments
。
班级评论:
@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?如果是,我的代码有什么问题?
谢谢