0

I have a resource that needs to be exposed through a restful service. I want it to have multiple revisions of a resource(every time it is updated). I have id(resource id) and a subid (revision id)

id 1 subid 0 
id 1 subid 1
id 1 subid 2
id 2 subid 0

In a post here I tried with an auto-increment resource id and a sub-id (incremented programmatically on each update) Table

 CREATE TABLE `SomeEntity` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `subid` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`,`subid`),

Entity

private long id;
private int subid;

@Id
@Column(name = "id")
public long getId() {
    return id;
  }

  @Id
  @Column(name = "subid")
  public int getSubid() {
    return subid;
  }

As in answer in linked post One available solution is to have a extra column concatenation of id and subid which allows fetching the assigned id on create.

Is there any information available on writing a custom SelectGenerator to achieve the purpose of incremental id and subid?

If i give away auto-increment for id.What would be a better way to achieve this?

Community
  • 1
  • 1
rakesh99
  • 1,234
  • 19
  • 34

1 Answers1

0

I think you'll need to do a hack to support this scenario, as mysql (nor JPA) supports this,

The hack is the following: You need a table with only on autoincrement column and map it to an entity (let's call it SomeEntityIdGenerator). This entity will provide you with the id field of SomeEntity.

Then from SomeEntity, you'll need to change the composite key to have a ManyToOne relationship to SomeEntityIdGenerator.

I'm not claiming that this is pretty (because it's very ugly), but I think it's the only way to do it with plain JAP/SQL. There might be an alternatives using triggers, but you'll still need the extra table to work as a sequence.

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • I can achieve a similar thing by having an @EmbeddedId in SomeEntity and using some generator code to assign embeddedId.id in create and increment version of embeddedId.version in update.With this table can you please explain how will be the id and version assigned to SomeEntity during create/update ? – rakesh99 Jul 26 '15 at 19:46
  • You'll need to handle the version youself (probably you can use a simple `@PrePersist` and `@PreUpdate`. The `SomeEntity.id` will be assigned automatically as it's a ManyToOne relationship, so hibernate will automatically assign the id. – Augusto Jul 26 '15 at 19:49
  • In create and update methods i use entitymanager,persist(..) .Will it assign an auto-increment id only when an id is not present in the object i.e during create ?Also need to test if assigned id is available in object as i need to return assigned id in create.As in other post without @GeneratedValue the assigned id was not reflected in object – rakesh99 Jul 26 '15 at 19:58
  • Additional note please make sure that you have proper transaction and locking mechanism in place. Imagine concurrent update requests for the same record. Also don't forget that you may end up deploying it in a cluster. If not thought out properly you may end up getting duplicate key exceptions while updating your entity instances. – Suken Shah Jul 27 '15 at 04:23