I have "post" entity/table with this "schema":
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer postId;
private Integer revisionId;
private Boolean isCurrentRevision;
So, table contains posts, each of them has multiple revisions, but only of them (for each post) is current.
Now, let's say I want to persist another revision of existing post (i.e. update post):
I need to find highest existing revisionId for this postId, increment it and set it to revisionId. Also this is the new current revision and so it should be marked accordingly, but also the former current revision should be unmarked.
But how can I do this? I feel that this should really be part of entity implementation but on the other hand I need EntityManager to do this. But I can't find a way to inject EntityManager instance (which is guaranteed to exist).
Is it possible? How do you implement such scenarios? Thanks!