@Version column is not working out of the box with spring data jdbc Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!spring-data-jdbc dependency won't work on pom.xmlspring-data-jdbc boolean column mapping to char@Query not working Spring Data JDBC 1.0.0.M3Nested data structure with Spring Data JDBCHow to tweak NamingStrategy for Spring Data JDBCOneToMany Spring Data JDBCHow to use arrays with Spring Data JDBCOptimistic Locking in Spring Data JDBCSpring jdbc repository many-to-many conjunction table column namesHow to query more than one columns but not all columns with @Query but still use the Domain Data Model to map with Spring Data JDBC?
Can you stand up from being prone using Skirmisher outside of your turn?
What is this word supposed to be?
How to translate "red flag" into Spanish?
Are all CP/M-80 implementations binary compatible?
How long after the last departure shall the airport stay open for an emergency return?
How to open locks without disable device?
Mistake in years of experience in resume?
How to keep bees out of canned beverages?
Why did C use the -> operator instead of reusing the . operator?
Is Diceware more secure than a long passphrase?
A Paper Record is What I Hamper
Multiple fireplaces in an apartment building?
What is /etc/mtab in Linux?
Suing a Police Officer Instead of the Police Department
Is Electric Central Heating worth it if using Solar Panels?
Could moose/elk survive in the Amazon forest?
The art of proof summarizing. Are there known rules, or is it a purely common sense matter?
Did the Roman Empire have penal colonies?
How to get even lighting when using flash for group photos near wall?
Do I need to protect SFP ports and optics from dust/contaminants? If so, how?
Is Bran literally the world's memory?
Map material from china not allowed to leave the country
What ability score does a Hexblade's Pact Weapon use for attack and damage when wielded by another character?
Putting Ant-Man on house arrest
@Version column is not working out of the box with spring data jdbc
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!spring-data-jdbc dependency won't work on pom.xmlspring-data-jdbc boolean column mapping to char@Query not working Spring Data JDBC 1.0.0.M3Nested data structure with Spring Data JDBCHow to tweak NamingStrategy for Spring Data JDBCOneToMany Spring Data JDBCHow to use arrays with Spring Data JDBCOptimistic Locking in Spring Data JDBCSpring jdbc repository many-to-many conjunction table column namesHow to query more than one columns but not all columns with @Query but still use the Domain Data Model to map with Spring Data JDBC?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have my version column defined like this
@org.springframework.data.annotation.Version
protected long version;
With Spring Data JDBC it's always trying to INSERT. Updates are not happening. When I debug I see that, PersistentEntityIsNewStrategy
is being used which is the default strategy. It has isNew()
method to determine the state of the entity being persisted. I do see that version and id are used for this determination.
But my question is who is responsible to increment the version column after every save, so that when the second time .save()
is called, the isNew()
method can return false.
Should we do fire a BeforeSaveEvent
and handle the incrementation of Version column? Would that be good enough to handle the OptimisticLock
?
Edit
I added an ApplicationListener to listen to BeforeSaveEvent like this.
public ApplicationListener<BeforeSaveEvent> incrementingVersion()
return event ->
Object entity = event.getEntity();
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
Long version = baseDataModel.getVersion();
if (version == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(version + 1L);
;
So now the version column works, but rest of Auditable fields @CreatedAt, @CreatedBy,@LastModifiedDate and @LastModifiedBy
are not set!!
Edit2
Created a new ApplicationListener like below. In this case both my custom listener and Spring's RelationalAuditingListener are getting called. But still it doesn't solve the problem. Because the order of listeners[custom one followed by spring's] making the markAudited
to invoke markUpdated
instead of markCreated
, since the version column is already incremented. I tried to make my Listener be the LOWEST_PRECEDENCE
still no luck.
My custom listener here
public class CustomRelationalAuditingEventListener
implements ApplicationListener<BeforeSaveEvent>, Ordered
@Override
public void onApplicationEvent(BeforeSaveEvent event)
Object entity = event.getEntity();
// handler.markAudited(entity);
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
if (baseDataModel.getVersion() == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(baseDataModel.getVersion() + 1L);
@Override
public int getOrder()
return LOWEST_PRECEDENCE;
spring-data-jdbc
add a comment |
I have my version column defined like this
@org.springframework.data.annotation.Version
protected long version;
With Spring Data JDBC it's always trying to INSERT. Updates are not happening. When I debug I see that, PersistentEntityIsNewStrategy
is being used which is the default strategy. It has isNew()
method to determine the state of the entity being persisted. I do see that version and id are used for this determination.
But my question is who is responsible to increment the version column after every save, so that when the second time .save()
is called, the isNew()
method can return false.
Should we do fire a BeforeSaveEvent
and handle the incrementation of Version column? Would that be good enough to handle the OptimisticLock
?
Edit
I added an ApplicationListener to listen to BeforeSaveEvent like this.
public ApplicationListener<BeforeSaveEvent> incrementingVersion()
return event ->
Object entity = event.getEntity();
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
Long version = baseDataModel.getVersion();
if (version == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(version + 1L);
;
So now the version column works, but rest of Auditable fields @CreatedAt, @CreatedBy,@LastModifiedDate and @LastModifiedBy
are not set!!
Edit2
Created a new ApplicationListener like below. In this case both my custom listener and Spring's RelationalAuditingListener are getting called. But still it doesn't solve the problem. Because the order of listeners[custom one followed by spring's] making the markAudited
to invoke markUpdated
instead of markCreated
, since the version column is already incremented. I tried to make my Listener be the LOWEST_PRECEDENCE
still no luck.
My custom listener here
public class CustomRelationalAuditingEventListener
implements ApplicationListener<BeforeSaveEvent>, Ordered
@Override
public void onApplicationEvent(BeforeSaveEvent event)
Object entity = event.getEntity();
// handler.markAudited(entity);
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
if (baseDataModel.getVersion() == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(baseDataModel.getVersion() + 1L);
@Override
public int getOrder()
return LOWEST_PRECEDENCE;
spring-data-jdbc
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07
add a comment |
I have my version column defined like this
@org.springframework.data.annotation.Version
protected long version;
With Spring Data JDBC it's always trying to INSERT. Updates are not happening. When I debug I see that, PersistentEntityIsNewStrategy
is being used which is the default strategy. It has isNew()
method to determine the state of the entity being persisted. I do see that version and id are used for this determination.
But my question is who is responsible to increment the version column after every save, so that when the second time .save()
is called, the isNew()
method can return false.
Should we do fire a BeforeSaveEvent
and handle the incrementation of Version column? Would that be good enough to handle the OptimisticLock
?
Edit
I added an ApplicationListener to listen to BeforeSaveEvent like this.
public ApplicationListener<BeforeSaveEvent> incrementingVersion()
return event ->
Object entity = event.getEntity();
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
Long version = baseDataModel.getVersion();
if (version == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(version + 1L);
;
So now the version column works, but rest of Auditable fields @CreatedAt, @CreatedBy,@LastModifiedDate and @LastModifiedBy
are not set!!
Edit2
Created a new ApplicationListener like below. In this case both my custom listener and Spring's RelationalAuditingListener are getting called. But still it doesn't solve the problem. Because the order of listeners[custom one followed by spring's] making the markAudited
to invoke markUpdated
instead of markCreated
, since the version column is already incremented. I tried to make my Listener be the LOWEST_PRECEDENCE
still no luck.
My custom listener here
public class CustomRelationalAuditingEventListener
implements ApplicationListener<BeforeSaveEvent>, Ordered
@Override
public void onApplicationEvent(BeforeSaveEvent event)
Object entity = event.getEntity();
// handler.markAudited(entity);
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
if (baseDataModel.getVersion() == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(baseDataModel.getVersion() + 1L);
@Override
public int getOrder()
return LOWEST_PRECEDENCE;
spring-data-jdbc
I have my version column defined like this
@org.springframework.data.annotation.Version
protected long version;
With Spring Data JDBC it's always trying to INSERT. Updates are not happening. When I debug I see that, PersistentEntityIsNewStrategy
is being used which is the default strategy. It has isNew()
method to determine the state of the entity being persisted. I do see that version and id are used for this determination.
But my question is who is responsible to increment the version column after every save, so that when the second time .save()
is called, the isNew()
method can return false.
Should we do fire a BeforeSaveEvent
and handle the incrementation of Version column? Would that be good enough to handle the OptimisticLock
?
Edit
I added an ApplicationListener to listen to BeforeSaveEvent like this.
public ApplicationListener<BeforeSaveEvent> incrementingVersion()
return event ->
Object entity = event.getEntity();
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
Long version = baseDataModel.getVersion();
if (version == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(version + 1L);
;
So now the version column works, but rest of Auditable fields @CreatedAt, @CreatedBy,@LastModifiedDate and @LastModifiedBy
are not set!!
Edit2
Created a new ApplicationListener like below. In this case both my custom listener and Spring's RelationalAuditingListener are getting called. But still it doesn't solve the problem. Because the order of listeners[custom one followed by spring's] making the markAudited
to invoke markUpdated
instead of markCreated
, since the version column is already incremented. I tried to make my Listener be the LOWEST_PRECEDENCE
still no luck.
My custom listener here
public class CustomRelationalAuditingEventListener
implements ApplicationListener<BeforeSaveEvent>, Ordered
@Override
public void onApplicationEvent(BeforeSaveEvent event)
Object entity = event.getEntity();
// handler.markAudited(entity);
if (BaseDataModel.class.isAssignableFrom(entity.getClass()))
BaseDataModel baseDataModel = (BaseDataModel) entity;
if (baseDataModel.getVersion() == null)
baseDataModel.setVersion(0L);
else
baseDataModel.setVersion(baseDataModel.getVersion() + 1L);
@Override
public int getOrder()
return LOWEST_PRECEDENCE;
spring-data-jdbc
spring-data-jdbc
edited Mar 23 at 1:12
Seetharamani Tmr
asked Mar 22 at 15:46
Seetharamani TmrSeetharamani Tmr
376217
376217
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07
add a comment |
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07
add a comment |
1 Answer
1
active
oldest
votes
Currently, you have to increment the version manually and there is no optimistic locking, i.e. the version is only used for checking if an entity is new.
There is an open issue for support of optimistic locking and there is even a PR open for it.
Therefore it is likely that this feature will be available with an upcoming 1.1 milestone.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303302%2fversion-column-is-not-working-out-of-the-box-with-spring-data-jdbc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Currently, you have to increment the version manually and there is no optimistic locking, i.e. the version is only used for checking if an entity is new.
There is an open issue for support of optimistic locking and there is even a PR open for it.
Therefore it is likely that this feature will be available with an upcoming 1.1 milestone.
add a comment |
Currently, you have to increment the version manually and there is no optimistic locking, i.e. the version is only used for checking if an entity is new.
There is an open issue for support of optimistic locking and there is even a PR open for it.
Therefore it is likely that this feature will be available with an upcoming 1.1 milestone.
add a comment |
Currently, you have to increment the version manually and there is no optimistic locking, i.e. the version is only used for checking if an entity is new.
There is an open issue for support of optimistic locking and there is even a PR open for it.
Therefore it is likely that this feature will be available with an upcoming 1.1 milestone.
Currently, you have to increment the version manually and there is no optimistic locking, i.e. the version is only used for checking if an entity is new.
There is an open issue for support of optimistic locking and there is even a PR open for it.
Therefore it is likely that this feature will be available with an upcoming 1.1 milestone.
answered Mar 24 at 14:26
Jens SchauderJens Schauder
47.6k18115243
47.6k18115243
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303302%2fversion-column-is-not-working-out-of-the-box-with-spring-data-jdbc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you create a separate question for the problem with auditing, or describe why you think it is related to the version/optimistic locking issue? In any case please show the relevant code: attributes & configuration.
– Jens Schauder
Mar 24 at 14:28
The Auditable fields weren't related to version column increment nor the optimistic lock. It just clashed with the BeforeSaveEvent fired to handle the auditable fields, when I created a BeforeSaveEvent to handle the version column. But I see now that the PR is in place to handle the version column incrementation. Thanks!
– Seetharamani Tmr
Apr 3 at 15:07