30

I'm having trouble with my first steps using Spring-Boot with JPA. I've started with a pretty minimalistic example from Git using Gradle.

Now simply moving Customer to another package, let's say to hello2 results in an exception Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer. I tried to add

@ComponentScan(basePackageClasses= {Customer.class}) // AND OR @EnableJpaRepositories(basePackageClasses= {Customer.class})

to Application, but without success.

What am I doing wrong?

Stefan K.
  • 7,701
  • 6
  • 52
  • 64

4 Answers4

43

Location of entities in Spring Boot can be configured using @EntityScan.

By default, @EnableAutoConfiguration enables entity scanning in the package where it's placed (if it's not a default package).

mkobit
  • 43,979
  • 12
  • 156
  • 150
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1
    I am using this @SpringBootApplication @EntityScan(value="org.package.spike.entities") where org.package.spike.entities is the package that contains my entity classes still i get the same problem – Rafael Mar 29 '15 at 12:36
  • 1
    This is a couple years later, but i am also getting this same thing. Tried every possible config i can think of but am still getting "not a managed type" put @EnableAutoConfiguration and @EntityScan("my package") in my repository configuration and still get this error. – wondergoat77 Jul 24 '17 at 15:24
35

You must locate entities and repositories pakages by using

@EnableJpaRepositories(basePackages = "your.repositories.pakage")

@EntityScan(basePackages = "your.entities.pakage")
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
tranductrinh
  • 470
  • 4
  • 11
  • I was missing @EntityScan, my entities and my repos are in different packages. You can alternatively use @EntityScan(basePackageClasses = AClassInPackageWhereAllEntitiesAre.class) Thanks! – Toguard Oct 25 '15 at 09:24
7

this is what worked for me :

@EnableJpaRepositories(basePackages ={ "package1","package2"})
@EntityScan(basePackages ={ "package3","package4"})
slfan
  • 8,950
  • 115
  • 65
  • 78
bhaskar babu
  • 121
  • 2
  • 2
1

Giving same package location (i.e base package) for below annotation worked for me :-

@SpringBootApplication(scanBasePackages = {"org.ashu.java.*"})
@EnableJpaRepositories(basePackages ={ "org.ashu.java.*"})    
@EntityScan(basePackages ={ "org.ashu.java.*"})
Ashutosh Shukla
  • 557
  • 5
  • 9