简体   繁体   中英

Spring CommandLineRunner Interface Implementation

So I was following the tutorial here: https://spring.io/guides/gs/accessing-data-jpa/

and it works fine, I'm trying to implement it in my application (Because it makes JPA so easy to use), but I'm confused.

Where it has

 @Bean
 public CommandLineRunner demo(CustomerRepository repository)

and then it acts on the repository

repository.save(new Customer("Jack", "Bauer"));
        repository.save(new Customer("Chloe", "O'Brian")); 

How can it act on an interface? CustomerRepository is an interface, and I can't instantiate it

CustomerRepository c = new CustomerRepository() 

cant be done, and I don't have any classes that implement the interface. I just wanted to do something like

 CustomerRepository c = new CustomerRepository() 
 c.save(new Customer("whatever", "whatever")

but I can only use it in the CommandLineRunner bean method. Why can I do this with commandlinerunner but cant do it with another class?

I was thinking I could just make a class that extends CustomerRepository, but then I read that the interface actually does all the method implementation itself (JPA does it) so you don't have to worry about it.

public interface CustomerRepository extends CrudRepository<Customer, Long> {

List<Customer> findByLastName(String lastName);
}

so if I extended it, wouldn't I have to override the findbylastname() method, meaning JPA wouldn't do it itself?

Thanks for any assistance.

I extended it, wouldn't I have to override the findbylastname() method, meaning JPA wouldn't do it itself ?

No, you don't need to implement the methods as spring-data-jpa will take care of it, you can look here on how Spring data repository interfaces are actually implemented by proxy at runtime .

The main point that you need to remember is that spring data has got few rules to derive the sql queries from the method names (like findByLastName() , findByLastnameOrderByFirstnameAsc() , etc..), you can look here to understand how method names work and they are related to field names of your entity bean .

If you wanted to write some complex queries which can't be derived from method names you can use @Query for your methods.

If I made a class public class Cust implements CustomerRepository what would I do when it asks me I have to implement the findByLastName(String lastName); method that JPA is supposed to take care of ?

If you wanted to implement repository to provide your custom behaviour for few of the methods for few of your methods, you can do that (like class Cust implements CustomerRepository ), you can refer the section "Using JpaContext in custom implementations", it is well explained in the ref doc.

so if I extended it, wouldn't I have to override the findbylastname() method, meaning JPA wouldn't do it itself?

No, it is not JPA which does the job but Spring which generates by APO some JPA processings.

With Spring Repository, you have multiples ways of doing :

  • write your own SQL/JPQL query.
  • use naming convention in the method name to write the query

In both cases, you don't need to implement directly the interface you declare here :

public interface CustomerRepository extends CrudRepository<Customer, Long> {   
   List<Customer> findByLastName(String lastName);
}

Because as you understood, the job is performed for you by Spring.
For example, in the case you quote, you use naming convention in the method name to write the query.

When Spring inspects your interface and sees the method findByLastName(String lastName) , it associates the method name to a query which does a find with a match by lastName field. So, it generate a JPQL query like that :

select c from Customer c where c.lastName=%lastName

and it sets the lastName param with the effective parameter used when the method is call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM