Skip to content
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

Save entity with custom UUID identifier #853

Closed
detomarco opened this issue Feb 14, 2024 · 2 comments
Closed

Save entity with custom UUID identifier #853

detomarco opened this issue Feb 14, 2024 · 2 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@detomarco
Copy link

detomarco commented Feb 14, 2024

Hello,

I need to persist an object with UUID generated code side.

@Table("cars")
public class Car {
    @Id
    private UUID id;
    private String name;
    // getters - setters
}

public interface CarRepository extends ReactiveCrudRepository<Car, UUID> {
}


@Test
void contextLoads() {
     Car car = new Car();
     car.setId(UUID.randomUUID());
     car.setName("BMW");

     Mono<Car> saved =  carRepository.save(car);
     saved.block();
}

but I get this error


org.springframework.dao.TransientDataAccessResourceException: Failed to update table [cars]; Row with Id [648e8774-0a5a-49b9-9c93-d435403d0050] does not exist
	at org.springframework.data.r2dbc.core.R2dbcEntityTemplate.lambda$doUpdate$12(R2dbcEntityTemplate.java:640)
	at reactor.core.publisher.FluxHandle$HandleSubscriber.onNext(FluxHandle.java:113)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
	at reactor.core.publisher.MonoUsingWhen$MonoUsingWhenSubscriber.deferredComplete(MonoUsingWhen.java:268)
	

In my understanding, spring-data-r2dbc uses id to check if the entity is already persisted and it fails when id is set but not found in the database.
Is this an expected behaviour/limitation of the library?
The only way I found to achieve this is to insert the object manually via DatabaseClient, which is inconvenient in my opinion

Here the project to reproduce the issue
Spring boot version: 3.2.2

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 14, 2024
@rrrship
Copy link

rrrship commented Feb 15, 2024

Try to add @Version property to your entity. I think then it will check based on the version if it's a new entity or not, and it doesn't try to update it.

@mp911de
Copy link
Member

mp911de commented Feb 20, 2024

Is this an expected behaviour/limitation of the library?

It is indeed as we fall back to the identifier to determine whether to insert or update an entity. Check out the documentation on Entity State Detection Strategies.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
@mp911de mp911de added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

4 participants