0

Problem description: I am using JPA's R2dbcRepository for the storage of entities, but I am having problems with the storage of entities. I manually set the primary key ID of the entity, but the save operation was unsuccessful and no exceptions were thrown. Here is my code example:

public class ServiceImpl <M extends R2dbcRepository<T, ID>,T,ID> implements IService<T,ID> {

    @Autowired
    protected M baseRepo;

    @Autowired
    protected R2dbcEntityTemplate template;

    @Override
    public Mono<T> save(T entity) {
        return baseRepo.save(entity).doOnError(err->{
            System.out.println(err.toString());
            throw new RuntimeException(err);
        });
    }
}

Expected Behavior: I expect the entity to be saved to the database by calling the save() method.

Actual Behavior: However, the entity is not successfully saved to the database, and no exceptions are thrown.

Attempted Solutions:

I found that the cause of this was that I set the primary key ID, which resulted in an update operation instead of an insert operation. I have consulted relevant documentation and resources but haven't found a solution to this issue.

Additional Information:

Database driver being used: [org.springframework.boot:spring-boot-starter-data-r2dbc,dev.miku:r2dbc-mysql:0.8.2.RELEASE] Database configuration: [url: r2dbc:mysql://127.0.0.1:3306/pets?useSSL=false]

Noah
  • 1

1 Answers1

0

I found the way to solution it.Let the entity implement Persistable such as:

public class User implements Persistable<String> {
@Transient
private boolean method = true;

public void setMethod(boolean method) {
    this.method = method;
}

@Override
public boolean isNew() {
    return method;
}

}

Noah
  • 1