spring-projects / spring-projects/spring-data-relational

Decide "isNew" logic with invoked method when using "insert" and "update"

Open
#1,444 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for: team-attention type: bug
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Hello!

I would like to ask and clarify a situation about using JdbcAggregateOperations methods which are not referenced in "CrudRepository".

Suppose that we created a new CrudRepository interface and we want to use "insert" and "update" individually.

@NoRepositoryBean
public interface MyRepository<T, ID> extends CrudRepository<T, ID> {

  /* MyRepository */

  T insert(T instance);

  T update(T instance);

}

Corresponding implementation as follows


@Transactional(readOnly = true)
public class MyJdbcRepository<T, ID> implements MyRepository<T, ID> {

  ... 

  @Transactional
  @Override
  public T insert(T instance) {
    return entityOperations.insert(instance);
  }

  @Transactional
  @Override
  public T update(T instance) {
    return entityOperations.update(instance);
  }
  
 ...
  
} 

Now I will create a simple pojo with Audit specialitiy.

public class Auditing {

  @Id
  private String id;

  private String name;

  @CreatedDate
  private LocalDateTime createdAt;

  @LastModifiedDate
  private LocalDateTime lastUpdatedAt;

  ...
}

Assume we configured annotation based enabling for both repo and auditing. If we run a simple test like this:

  @Test
  public void shouldAuditTimes() {
    Auditing auditing = new Auditing();
    auditing.setId(UUID.randomUUID().toString());
    auditingRepository.insert(auditing);
    ...
}

SDR is invoking "RelationalAuditingCallback" and determining isNew from IsNewAwareAuditingHandler. But, this handler behaves like we called "save" method and trying to resolve it from "org.springframework.data.mapping.PersistentEntity#isNew".

As you can see, I am setting "id" of object so resolved behaviour is "modify". But I am inserting (creating) and calling the "insert" method.

IMHO, this resolving mechanism should honor root method (the actual behaviour "insert" or "update") before checking it from PersistenceEntity. What do you think?

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing JdbcAggregateOperations insert and update into RelationalAuditingCallback and IsNewAwareAuditingHandler, using the issue's Auditing example with an assigned id. Determine whether the invoked operation is available when auditing decides isNew. Done means the behavior for insert and update is defined and covered by tests, but the issue does not name existing files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.