Sumedha
Sumedha

Reputation: 163

How JpaRepository.save() method work internally

I have an entity - Workflow

@Entity
Workflow {
    ...
    ...
    @OneToMany(mappedBy = "workflow", cascade = CascadeType.ALL)
    List<Tasks> tasks
    ...
}

And another Task entity with

@Entity
Task {
    ...
    Status status;
    ...
    @ManyToOne
    @JoinColumn(name = "workflow_id")
    Workflow workflow
    ...
}

Both entities have defined JpaRepository.

WorkflowRepo extends JpaRepository<Workflow, Id>
TaskRepos extends JpaRepository<Task, Id>

Now, to update the status of Task which method is efficient?

workFlowRepo.save(workflow) // saving the whole workflow object which internally updates task also 

Or

tasRepo.save(task) // saves only task object in repo.

Is there any major performance difference between the above two techniques.

i.e. How does JpaRepoitory.save() will work in these conditions? Will it run the update statement on the whole thing or just update the changed object?

Upvotes: 4

Views: 8556

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81907

Since you don't have cascade annotations you have to save the task.

If you had an appropriate cascade annotations the performance difference depends on a ton of things like the exact mapping of the entities, the state of the entities and the state of the underlying EntityManager. In general it will be neglectable though compared to the unavoidable time needed to actually save the entities.

Regarding how save works:

The save operation performs a merge on the underlying EntityManager. This on its own doesn't perform any SQL but just returns a managed version of the entity. If that isn't already loaded into the EntityManager it might do this or it might identify the entity as a new one and make it a managed entity. It then might have to perform an insert statement or a select in order to obtain an id.

If the saving didn't happen as a side effect of the above it will only happen when the EntityManager gets flushed. This typically happens at the end of the transaction but might also happen before a query gets executed or when explicitly asked for.

Note: It looks like you want a bidirectional relationship, but you actually have two separate relationships. One from Workflow to Task and one back from Task to Workflow.

Upvotes: 1

Related Questions