سؤال

There are two entities in my Google App Engine data store with Java, Objectify 4, i have a requirement in which the auto generated ids of these two entities should be unique, that is, id from first entity will never have an entry for second entity.

How to make sure that generated ids are not repeated for different entities. ?

In JPA if i am using Generated value strategy AUTO for ids, in database level, unique values are maintained, whether GAE also follow the same? I am currently using Objectify4 for persistence.

JPA Ref : AUTO strategy

هل كانت مفيدة؟

المحلول

If your entities are of the same kind (= same class in JPA), then auto-generated IDs are guaranteed to be unique.

Update: OP clarified that he needs unique IDs across different entity kinds.

You will need to use manually generated IDs - use allocateIds(kind, num) method where you use a made-up kind for all. Use num of 1 to generate only one ID.

long uniqueId = DatastoreService.allocateIds("MyUniqueUserKind", 1).getStart().getId();

All IDs generated this way are always unique as long as you always use the same kind, in this case "MyUniqueUserKind".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top