Sridhar Karuppusamy
Sridhar Karuppusamy

Reputation: 422

Spring JPA Save Method - Performance Issue

I have one performance problem in JPA save() method. I Have "CustomerHistory" Table it's like a log table I'm not even use this table data in my application. (I'm not perform any get query operations in the table). But If I save the "CustomerHistory" Model using JPA Save Method Means it's every time return the CustomerHistory Model After saving the data. So I need to know if it's play the big role in performance?

 public CustomerHistory saveCustomerHistoryLog(CustomerHistory  customerHistory ){
    return customerHistoryRepository.save(customerHistory );
}

Upvotes: 1

Views: 2222

Answers (3)

Sridhar Karuppusamy
Sridhar Karuppusamy

Reputation: 422

Here i found some there ways to improve performance in jpa save method:

Improve JPA Save Method Performances

Upvotes: 0

Ken Chan
Ken Chan

Reputation: 90517

If the CustomerHistory does not have any relationship to other entities , it should be okay.

But even if it has relationship to other entities , and if you are following the best practice that do not enable eager fetching when mapping the related entities , it should not affect the performance since they are lazy loaded and no unnecessary SQL will be triggered to load them if you do not access them.

Upvotes: 2

Siddharth Eswaramoorthy
Siddharth Eswaramoorthy

Reputation: 1026

I have faced a similar issue because of providing the @Id value generated in the application code.

Please make sure that the primary key column annotated with @Id should be null and generated using @GeneratedValue(strategy = GenerationType.AUTO). Then JPA consider it as a new record.

If you provide the primary key column in the code, then JPA consider it as update so it will do a fetch for doing the update/insert based on the fetch result. In that case there may be a latency.

If all you are doing with that table is inserting the record, then provide the @Id column as null and auto generated.

Upvotes: 1

Related Questions