-
Notifications
You must be signed in to change notification settings - Fork 356
repository.save()
returns duplicate child-elements (at least w/ postgresql) [DATAJDBC-456]
#680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Jens Schauder commented The problem is in the construction of your class. Since it is annotated with Lomboks In this case I recommend one of the following mappings: Remove the Alternatively: rename the id column to e.g. |
Clemens Hahn commented Thank you for this investigation! Makes sense! This results in a few further questions about that topic: I need the ID-field on my sub-entities (will be exposed via a REST-API) - so removing this field is not an option for the moment. I'm a fan of immutability. So I would like to make all fields of my entities immutable (btw: normally I write all entities as kotlin data classes). I am using UUIDs as IDs. This IDs are not auto-generated by my database. Therefore the application must handle this. Currently I use a But because of Entity State Detection Strategies I have to work with a mutable ID-field (as far as I can survey spring-data-jdbc for now): Problems with an immutable ID-field:
I would appreciate if you could give me your thoughts about this situation and I hope this is not the wrong place for this discussion |
Jens Schauder commented
b) use c) use a version attribute on the aggregate root. This is only available in 2.0.0-SNAPSHOT releases and there are some breaking changes coming up so you probably don't want to use it in production yet. But for longer term planning this might be an option. The version attribute will be used for state checking and it will be increased on your entity and in the database for you. See DATAJDBC-219. Since we have some differentiation going on between aggregate roots and other entities I highly recommend reading this article about aggregates and their relevance for Spring Data JDBC if you haven't already |
Clemens Hahn commented
{purchaseOrderId} }} and {{GET /purchases/{purchaseOrderId}/items/ {orderItemId}}} or even {{GET /orderitems/{orderItemId} }}. (I believe this point is out of scope of this discussion. But I appreciate your input!)
3a. Thank your for suggested the 3c. Using spring-data-jpa I liked the approach to detect the entity state via the version-field! As I noticed that Optimistic Locking is not yet supported by spring-data-jdbc that was not an option for me anymore. I'm happy that you make progress in that point and as soon as it become stable I will give it a try! I played with all this input in an example project. This example is written in kotlin. It extends your spring-data-example project.
|
Clemens Hahn commented Sorry - Jira destroyed the format of my API-Endpoints (in 1.) and I'm not allowed to edit it: GET /purchases/{purchaseOrderId}
GET /purchases/{purchaseOrderId}/items/{orderItemId}}}
GET /orderitems/{orderItemId} |
Clemens Hahn opened DATAJDBC-456 and commented
I will explain this behavior based on the [example-project|https://github.com/spring-projects/spring-data-examples/blob/master/jdbc/basics/README.adoc].
I added a new child-entity to the
LegoSet
-Entity of typeColor
.LegoSet
references this entity viaSet<Color> colors
.LegoSet
Color
legoSetRepository.save(legoSet)
and inspect the returned valueSet<Color> colors
contains 2 Colors.This does not happen using the embedded H2-Database! To reproduce it you can use a Postgresql Database!
To (hopefully quickly) reproduce this bug please apply the attached patch-file to the example-repo and execute the following (manual) steps:
docker run --name spring-data-jdbc-kotlin-test -e POSTGRES_USER=integrationtest -e POSTGRES_PASSWORD=password -e POSTGRES_DB=spring-data-jdbc-kotlin -p 5438:5432 -d postgres:11
src/main/resources/schema.sql
(using a database-tool)example.springdata.jdbc.basics.aggregate.AggregateTests#exerciseSomewhatComplexEntity
Attachments:
The text was updated successfully, but these errors were encountered: