Tomek Tomek - 6 months ago 83
SQL Question

Spring Boot doesn't create automatic configuration for database from application.properties

I have followed guide for creating simple spring boot app with jpa and it should have create database connection based on configuration from application.properties file, however it doesnt work(I get Not an managed type exceptions). I know this is issuse with application.properties file because app runs when I configure it manually like here.

Project structure:
Project structure

POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo.tomek</groupId>
<artifactId>SpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>


</dependencies>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>




application.properties file:

spring.datasource.url = jdbc:mysql://localhost:3306/TEST?createIfNotExists=true
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=but

spring.jpa.show-sql=true

# Enable spring data repos
spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update

spring.thymeleaf.cache=false
spring.template.cache=false


static void main()

@SpringBootApplication
@ComponentScan("demo")
@EnableJpaRepositories("demo")
public class SpringBootTEST {

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringBootTEST.class, args);

ProductService pRepo = ctx.getBean("productService", ProductService.class);
pRepo.addSomeProducts();
CustomerService cRepo = ctx.getBean("customerService", CustomerService.class);
cRepo.addSomeCustomers();
}
}


Let me stress it again: everything works when I configure it manually like here: http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/

I run out of ideas...

Answer

Since your entities are not in a subpackage of your application class, you also have to add the following annotation to SpringBootTEST so that Hibernate can scan your entity classes:

@EntityScan("demo.database")

Alternatively, it should also work, if you specify the package in the SpringBootApplication annotation:

@SpringBootApplication("demo")

Then you should also be able to remove the custom @ComponentScan and @EnableJpaRepositories annotation.