antony.trupe
antony.trupe

Reputation: 10824

Google App Engine Datastore multi-field key

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable ="false")
public class Foo implements IsSerializable {

 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Long id;

    @Persistent
    private Long revision;

    @Persistent
    private String information;
}

The problem is this object keeps overwriting itself when persisted, not creating a new 'record' with the next revision. In a traditional RDBMS it'd be a two-column primary key.

How do I accomplish this with Google App Engine Datastore?

Upvotes: 0

Views: 898

Answers (2)

antony.trupe
antony.trupe

Reputation: 10824

I think this is the best way to solve it.

@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable = "false")
public class Foo implements IsSerializable {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long _internalId;

  @Persistent
  private Long id;

  @Persistent
  private Long revision;

  @Persistent
  private String information;
}

where id and revision are treated as the primary key in the application.

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101139

You need to create and write a new record for each update if you want to keep a revision history. The id uniquely identifies the record - the Datastore has no way of knowing that you consider the revision to be part of the id too.

Upvotes: 1

Related Questions