Hibernate+JPA Multi-version concurrency control with Postgres

If you are working with Postgres and using JPA via Hibernate, here’s a short cut to get MVCC without having to add an extra column to your database tables:

@Column(name = "xmin", columnDefinition = "xid")
@Version
@Generated(GenerationTime.ALWAYS)
private int version;

Postgres automatically creates and maintains the “xmin” column, and JPA already supports the concept of @Version columns. Hibernate adds on support for database-generated Version columns. Now, xmin is really a “xid” type, but it happily accepts casting to and from Java’s int type.

Leave a Reply

Your email address will not be published.