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?

4 Answers 4

44

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
35

You must locate entities and repositories pakages by using

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

@EntityScan(basePackages = "your.entities.pakage")

1 Comment

I was missing @EntityScan, my entities and my repos are in different packages. You can alternatively use @EntityScan(basePackageClasses = AClassInPackageWhereAllEntitiesAre.class) Thanks!
7

this is what worked for me :

@EnableJpaRepositories(basePackages ={ "package1","package2"})
@EntityScan(basePackages ={ "package3","package4"})

Comments

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.*"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.