我已经跟随a guide创建了使用JPA的简单spring启动应用程序,当应用程序完成时,应该根据application.properties文件中的配置创建数据库连接,但是它没有发生(我得到了) 不是托管类型例外)。我知道这是与application.properties文件一起使用的,因为当我像here那样手动配置app时,app会正确运行。
的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文件:
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();
}
}
让我再次强调一下:当我手动配置它时,一切正常:http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/
我没有想法......
答案 0 :(得分:1)
由于您的实体不在应用程序类的子包中,因此您还必须将以下注释添加到SpringBootTEST
,以便Hibernate可以扫描您的实体类:
@EntityScan("demo.database")
或者,如果您在SpringBootApplication
注释中指定包:
@SpringBootApplication("demo")
然后您还应该能够删除自定义@ComponentScan
和@EnableJpaRepositories
注释。