Moving Spring Accessing Data with JPA getting started guide to something more complex -


i able sample application running on machine directly website: https://spring.io/guides/gs/accessing-data-jpa/.

this sample application gets started simple implementation of h2 embedded database.

it requires 2 dependencies run:

dependencies {    compile("org.springframework.boot:spring-boot-starter-data-jpa")    compile("com.h2database:h2")    testcompile("junit:junit") } 

the repository declared here reference:

package hello; import java.util.list; import org.springframework.data.repository.crudrepository;  public interface customerrepository extends crudrepository<customer, long> {     list<customer> findbylastname(string lastname); } 

this repository autowired configuration. files contained in same package/directory. i'm assuming spring smart enough instantiate correct instance of bean implementing customerrepository provides right connections h2database. i'm not sure how done here, works.

the code here:

package hello;  import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.commandlinerunner; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication;  @springbootapplication public class application implements commandlinerunner {      @autowired     customerrepository repository;      public static void main(string[] args) {         springapplication.run(application.class);     } } 

when runs looks default, it's building jpa container default hibernate persistence information , runs fine.

however, when decide use code baseline , move customerrepository package, jpa, i'm no longer able autowire repository application.

adding @componentscan("jpa") application not help:

.nosuchbeandefinitionexception: no qualifying bean of type [jpa.customerrepository]

adding @enablejparepositories("jpa") application yields different error:

illegalargumentexception: not managed type: class jpa.customer

so, looks can away minimalistic configuration jpa/hibernate/h2 long of relevant classes in same package.

my question is, have need move toward more complicated configuration when want move things different packages, or there preserve minimalism still able split things apart.

the easiest keep application class in root package of package hierarchy , move other classes sub-packages.

org.example      |       --- application.java      |       --- jpa      |   |      |    --- customerrepository.java      |       --- model          |           --- customer.java 

alternatively, if want every class in sub-package of own, such as:

org.example      |       --- bootstrap      |   |      |    --- application.java      |       --- jpa      |   |      |    --- customerrepository.java      |       --- model          |           --- customer.java 

you need use @entityscan well.

@springbootapplication @enablejparepositories("org.example.jpa") @entityscan("org.example.model") public class application ... 

see official documentation details.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -