We are writing a new application against an existing database. I am using Spring Data JPA and just doing
MyRepository.save()
for my new object using
MyRepository extends CrudRepository<MyThing, String>
I noticed in the logs that hibernate makes a choice before inserting, and that they take a lot of time, even if the queries use a primary key or index. I already asked a question on how to stop SELECT before insertion. I do not understand this. Now I'm just trying to figure out why so long.
I have a set of logs, so I see sql generated by hibernate. In one example, according to the registration time, this request takes about 4 seconds. This request requests a primary key. When I run sql, which hibernate generated in a db tool like dbvisualizer is returned to the hook, as expected.
I increased the sleep log to TRACE to see where the delay was, and found the following before and after the 4 second delay:
2017-08-11 13:45:40.099 TRACE 16136 --- [nio-8085-exec-1] o.h.l.p.e.i.AbstractLoadPlanBasedLoader : Bound [2] parameters total
2017-08-11 13:45:44.023 TRACE 16136 --- [nio-8085-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl : Registering result set [SQLServerResultSet:6]
I am also wondering why it says 2 parameters are related when there was only 1 in sql.
Any ideas why these samples take so long? Especially in my Spring application, and not in another client like dbvisualizer?
EDIT: adding more details, sql, entity code
, ; , , Hibernate SELECT INSERT, , . 4 .
sql:
select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from my_customer_table customer0_ where customer0_.customer_id=?
:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "my_customer_table")
public class Customer {
@Id
private String customer_id;
private String first_name;
private String last_name;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.first_name = firstName;
this.last_name = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
customer_id, first_name, last_name);
}
}
CustomerRepository
import com.....Customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, String> {
}
@Service, CustomerRepository @Autowired :
Customer customer = customerRepository.findOne(customerId);