Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot w/ JPA: move @Entity to different package

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?

like image 868
Stefan K. Avatar asked Apr 29 '14 13:04

Stefan K.


People also ask

What is the purpose of @entity in JPA?

An entity can aggregate objects together and effectively persist data and related objects using the transactional, security, and concurrency services of a JPA persistence provider.

What is the difference between save and saveAndFlush in JPA?

Save and saveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.

What is the use of @entity in spring boot?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.

Can JPA entity implements interface?

No, not possible with JPA or Hibernate. It does seem strange, when coding in the Java language which allows for attributes to be interfaces, that a persistence standard like JPA, intended for Java, does not support persisting attributes that are interfaces.


2 Answers

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

like image 184
axtavt Avatar answered Sep 30 '22 10:09

axtavt


You must locate entities and repositories pakages by using

@EnableJpaRepositories(basePackages = "your.repositories.pakage")  @EntityScan(basePackages = "your.entities.pakage") 
like image 33
tranductrinh Avatar answered Sep 30 '22 09:09

tranductrinh