bvdb
bvdb

Reputation: 24710

hibernate entity discovery only works inside the same folder

I have a Spring Boot project with Hibernate.

Nevertheless, all works well.

However, when I start adding new hibernate entities, then things go wrong. For some reason, the system only finds them when I put them inside the package of the other hibernate entities.

So, this leads me to believe that I do need additional configuration to help the auto-discovery mechanism. But what is the state-of-art in 2020 ? (I assume that the above xml files are now deprecated).

Upvotes: 0

Views: 386

Answers (2)

bvdb
bvdb

Reputation: 24710

I totally overlooked these annotations which were present on the SpringBootApplication class.

@SpringBootApplication(scanBasePackages = {"com.domain.foo.bar.*"})
@EnableJpaRepositories(basePackages ={"com.domain.foo.bar.*"})
@EntityScan(basePackages ={"com.domain.foo.bar.*"})
public class SpringBootApplication extends SpringBootServletInitializer {
}

I needed to add my packages here.

Upvotes: 1

Daimon Cool
Daimon Cool

Reputation: 98

If you use spring + hibernate then it solved by @ComponenScan annotation.

If pure hibernate then I think you need persistence.xml

EntityManager is the class that performs database interactions in JPA. It is initialized through a configuration file named persistence.xml. This file is found in the META-INF folder in your CLASSPATH, which is typically packaged in your JAR or WAR file. The persistence.xml file contains:

  • The named "persistence unit," which specifies the persistence framework you're using, such as Hibernate or EclipseLink.
  • A collection of properties specifying how to connect to your database, as well as any customizations in the persistence framework
  • A list of entity classes in your project

Upvotes: 1

Related Questions