Why Spring JPA repository save method executes select query first and then insert query ?

Below code snippet shows the implementation of save method of JPARepository.

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
	 */
	@Transactional
	@Override
	public <S extends T> S save(S entity) {

		Assert.notNull(entity, "Entity must not be null.");

		if (entityInformation.isNew(entity)) {
			em.persist(entity);
			return entity;
		} else {
			return em.merge(entity);
		}
	}

So, when we hit the save method of JPARepository, it first checks if the given entity already exists or not by hitting select query and if not exists only then executes insert else it updates the existing record.

How do I stop executing select query ?

If you are sure that the entity you are trying to persist is always new one and no need to hit select query, then get the entity object implement the persistable method. You need to implment isNew() method which should return true always.

@Entity
public class Student implements Persistable<Object>{
@Override
 public boolean isNew() {
	return true;
 }
}

Now, execute your repository save() method. Only insertion will happen now.