java - GAE 数据存储区使用 JPA 生成字符串 key

标签 java google-app-engine jpa google-cloud-datastore

由于不可能仅使用长 ID,因此我尝试使用生成的字符串键。我有三个类 UserTopicCommentsUser - 1:n - Topic - 1:n -注释

类(class)评论:

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

类(class)用户:

@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?如果是的话我的代码有什么问题吗?

谢谢

最佳答案

IIRC IDENTITY 策略是生成数字(或键)id。 如果您使用 JDO,则可以使用自动生成的 UUID 样式 id。 请参阅https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata#Keys

关于java - GAE 数据存储区使用 JPA 生成字符串 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13066019/

相关文章:

java - 过程代码和领域驱动设计风格代码有什么区别?

java - Android 中的安全性 - Google 应用引擎系统

java - 应用程序引擎预热请求不适用于端点

java - 如何在不使用 @PersistenceContext 注释的情况下以编程方式获取特定的 EntityManager

java - 从 IntelliJ 11 IDEA 中的实体生成表

java - 非 PK 列的 Hibernate ManyToOne JOIN 抛出 "Bad value for type int"

java - 根据Java中类成员的值对巨大的数组列表(ArrayList<Class>)进行排序

java - 按契约设计和类不变量

java - 如何使用 Jung 2 在图中用概率标记边

mysql - 如何使用 GQL 选择看起来像这样的组( key ('Group' 、 '1779_1528' )