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

Update domain model with generated non-id fields #1274

Open
romus opened this issue Sep 9, 2020 · 15 comments
Open

Update domain model with generated non-id fields #1274

romus opened this issue Sep 9, 2020 · 15 comments
Labels
type: enhancement A general enhancement

Comments

@romus
Copy link

romus commented Sep 9, 2020

Versions:

  • Spring boot 2.3.1.RELEASE
  • spring-data-r2dbc 1.1.1.RELEASE
  • r2dbc-postgresql 0.8.3.RELEASE

Current Behavior
check_create_at_ret table has a column created_at with a default value.

create table check_create_at_ret
(
    id         bigint generated by default as identity
        constraint check_create_at_ret_pkey primary key,
    created_at timestamp default now() not null,
    status     varchar(64)
);

But after saving the new entity, the value that is set at the database level is not returned.

Example:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CheckCreateAtRet {

    @Id
    private Long id;

    private String status;

    private LocalDateTime createdAt;
    
}
public interface CheckCreateAtRetRepo extends ReactiveCrudRepository<CheckCreateAtRet, Long> {
}
// ...
private final CheckCreateAtRetRepo checkCreateAtRetRepo;

public void save() {
        checkCreateAtRetRepo.save(new CheckCreateAtRet().setStatus("Status123"))
                .doOnNext(entity -> System.out.println(entity.getCreatedAt()))
                .subscribe();
}
// ...

Null will be printed

@mp911de mp911de added the status: duplicate A duplicate of another issue label Sep 10, 2020
@mp911de
Copy link
Member

mp911de commented Sep 10, 2020

Thanks for report. That's a duplicate of spring-projects/spring-data-r2dbc#440/#433. Can you retest against the latest snapshots?

@romus
Copy link
Author

romus commented Sep 10, 2020

Thank you for reply!

But I was able to reproduce the behavior on version 1.2.0-SNAPSHOT
Demo app: https://github.com/romus/r2dbc-demo

@mp911de mp911de removed the status: duplicate A duplicate of another issue label Sep 11, 2020
@mp911de
Copy link
Member

mp911de commented Sep 11, 2020

Thanks for the sample. I somehow was sidetracked because we had an issue where auditing and the created date was not populated. This case is different and it was never really considered to be working except for auto-generated Id's.

Postgres echo's the inserted row which allows consuming the generated fields.

@mp911de mp911de added the type: enhancement A general enhancement label Sep 11, 2020
@mp911de mp911de changed the title Default field is not updated Update domain model with generated fields Sep 11, 2020
@romus
Copy link
Author

romus commented Sep 12, 2020

Thanks.

@mp911de mp911de changed the title Update domain model with generated fields Update domain model with generated non-id fields Oct 27, 2020
@alesikov
Copy link

I'm facing the same issue and would like to add that also the default values for fields are not populated after save/update

@kzvankovich
Copy link

kzvankovich commented Dec 31, 2021

Hi all,

thee is a temporary solution. It possible to wire a custom query:

@Query("INSERT INTO table (attr1,attr2 ) VALUES (:attr1, :attr1) RETURNING *") Mono<Entity> create(attr1, Uattr1);

the following returning all clause is introduced: RETURNING *.

@energy2522
Copy link

any updates on this? It's still relevant and would be great to have a solution for this than providing workarounds.

@mp911de mp911de transferred this issue from spring-projects/spring-data-r2dbc Jun 27, 2022
@mp911de
Copy link
Member

mp911de commented Jun 27, 2022

This issue needs to be addressed in Spring Data Relational first.

@cemo
Copy link

cemo commented Jan 20, 2023

@mp911de is there any issue about it on Spring Data Relational?

@viniciusrd10
Copy link

Hi! there's some solution about this issue?

@schauder
Copy link
Contributor

schauder commented Sep 1, 2023

See #917

@Warfront1
Copy link

Warfront1 commented Sep 25, 2023

The best solution I currently have for this is just adding an additional .findBy repository call after each insert/update.
I'm not a fan of having to write a custom query for every insert/update as it significantly reduces the value of the built in springframework data repository.
The downside of using the .findBy approach I suggested is that you will be hitting the database twice.
Both workarounds suggested pollute the codebase and will need refactoring when this issue is fixed.

Had I known this was an issue from the start I would have likely just rolled a non reactive CrudRepository.
The worst part about this issue is how much this issue negatively impacts readability and/or performance of your application.

@Dwight-D
Copy link

Dwight-D commented Nov 16, 2023

This behavior is rather baffling. If the returned data from save() doesn't actually come from the database, it would make a lot more sense to have the return value be a Void type, as it is it's highly misleading and unintuitive. We already have access to the argument if we wanted to use that.

It also seems like the ID return doesn't work for certain types and certain databases. With postgres and r2dbc drivers, setting it to a UUID gives you back an entity with a null id, which makes this pretty much unusable.

CREATE TABLE broken_entity
(
    id UUID PRIMARY KEY DEFAULT gen_random_uuid()
);

  public class BrokenEntity {
      @Id
      // Will be populated by database default value
      UUID id;
  }

org.springframewoork.boot:spring-boot-starter-data-r2dbc:2.7.17
io.r2dbc:r2dbc-pool:0.9.2.RELEASE
org.postgresql:postgresql:42.3.8
org.postgresql:r2dbc-postgresql:0.9.2.RELEASE

@schauder
Copy link
Contributor

@Dwight-D The behaviour you describe with R2DBC is unrelated to this topic and sounds like a bug. Please create a separate issue for it.

@Dwight-D
Copy link

@Dwight-D The behaviour you describe with R2DBC is unrelated to this topic and sounds like a bug. Please create a separate issue for it.

Got it, thanks. Either way, I think it would be resolved if the save() call returned the actual data for the entire entity, which I think is the only sane behavior here from a user perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

10 participants