1

I have followed a guide for creating simple spring boot app with JPA and when application was completed it should have created database connection based on configuration from application.properties file, however it didn't happen (I get Not an managed type exceptions). I know this is issuse with application.properties file because app runs correctly 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 fine 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...

2
  • 1
    Move your SpringBootTEST class to the demo package remove all annotation but the @SpringBootApplication and restart. Commented May 18, 2016 at 6:09
  • @M.Deinum I tried it... and it also worked :) Commented May 18, 2016 at 16:25

1 Answer 1

1

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.

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

1 Comment

I was thinking @ComponentScan will handle it. I tried to delete other annotations also but it didnt work and I had to leave them as they were.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.