Open spring-projects-issues opened 7 years ago
Oliver Drotbohm commented
I think we can cut this short: this is not related to Spring Data REST at all but caused by your (partially correct) identifier setup. As documented, Spring Data inspects the identifier property and only treats entities with a null
, non-primitive value as new. As you hide this behind a manually handled property, the second call issues a merge(…)
which eventually results in an update.
As documented, you can tweak this by implementing Persistable
or rather switching to a dedicated auto-applied, synthetical identifier
Jianzhao Li commented
I'm sorry. I didn't realize I can use @Version
along with Persistable to solve this issue. Thank you very much!
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Jianzhao Li opened DATAREST-1007 and commented
As per Spring Data REST Documentation, POST method creates a new entity from the given request body. However, I found it could also update the existing entity. In some cases, this can be problematic. Here is an example:
DemoApplication.java
UserRepository.java
User.java
pom.xml (within project tag)
The result of isNew method will eventually effects save method in org.springframework.data.jpa.repository.support.SimpleJpaRepository.
In the situation mentioned in this question, the username field, which is also the id of the user entity, would always contain data in order to create new users. Therefore, when it goes to isNew, id == null will always return false. Then save method will always perform a merge operation
Affects: 2.6 GA (Ingalls)
Reference URL: http://stackoverflow.com/questions/42216997/post-duplicate-entry-not-causing-pk-collision-in-spring-data-rest