views:

303

answers:

2
@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?

+1  A: 

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.

antony.trupe
+1  A: 

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.

Nick Johnson