It's been a while since I posted this question but I've figured it out. I'll explain what I did so that it might be helpful to someone else.
The annotation @Version
is a javax.persistence interface and not the spring-data rest jpa framework as i mentioned earlier.
If you want to make use of @Version you need to create an version field in your domain object like so:
@Version
@Column(name = "VERSION")
private long version;
If you're using hibernate it will automatically pickup the annotation and it will create a "version" row in your (in my case MySql) table. Every time a record gets updated, hibernate will increment the counter with 1.
Now why is this something you want? Well the reason why you might wanna use this is because it decreases the chance that your clients are working with stale data. Whenever a client retrieves information from you a version is provided with the data he requested. e.g.
{ <-- School entity -->
"id": 1,
"version": 0,
"name": "De regenboog",
"street": "Plantaanstraat",
"number": "2",
"zipCode": "1234AS",
"city": "Amsterdam"
}
Now if a client wants to change some information about this specific record it sends the new information along with the version value. In this case let's change the name of the school.
{ <-- School entity -->
"id": 1,
"version": 0,
"name": "Stackoverflow",
"street": "Plantaanstraat",
"number": "2",
"zipCode": "1234AS",
"city": "Amsterdam"
}
Hibernate comes up with a query with your information and adds an extra 'where' clause to check the version. update .... where id = 1 and version = 0
. Now if the row is updated it means you provided the right version and no one else has changed that specific information between the time you requested the information, changed it and sent it back. Nice right?
Now what if the row isn't updated? It means someone else updated that row while you were taking a quick bathroom break after you requested the information. It means your version is outdated! What needs to happen now is really use case specific so I won't go into details about that :)
Hope someone can use this piece of information!
Thanks all
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…