我是Spring Boot和JPA的新手,我按照教程学习,直到我尝试使用Criteria(构建复杂的动态查询)为止,一切进展顺利。
首先,这是pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.scalian.app</groupId>
<artifactId>atelier</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ServerModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/resources/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/ClientModule/src/main/web/dist/clientapplication/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是我的主要应用程序类别:
package com.scalian.app.atelier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.scalian.app.atelier.persistence.repositories.EnvironmentCriteriaBuilder;
@SpringBootApplication
public class AtelierApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(AtelierApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EnvironmentCriteriaBuilder environmentCriteriaBuilder = new EnvironmentCriteriaBuilder(); //Error when adding this line :(
}
}
这是EnvironmentCriteriaBuilder类:
package com.scalian.app.atelier.persistence.repositories;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
public class EnvironmentCriteriaBuilder {
public EnvironmentCriteriaBuilder() {
EntityManager entityManager = Persistence.createEntityManagerFactory("atelierPersistenceUnit").createEntityManager();
entityManager.close();
}
}
这是我制作的配置类:
package com.scalian.app.atelier;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.scalian.app.atelier.persistence.repositories", entityManagerFactoryRef = "atelierEntityManagerFactory",transactionManagerRef = "atelierTransactionManager")
public class AtelierConfig {
@Bean
@Primary
public DataSource firstDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/atelier");
dataSource.setUsername( "username" );
dataSource.setPassword( "userpassword" );
return dataSource;
}
@Bean
public DataSource secondDataSource() {
return null;
}
@Bean(name="atelierEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(firstDataSource());
em.setPersistenceProviderClass(org.hibernate.jpa.HibernatePersistenceProvider.class);
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setPackagesToScan("com.scalian.app.atelier.persistence.entities");
em.setPersistenceUnitName("atelierPersistenceUnit");
em.setJpaProperties(additionalProperties());
em.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver());
return em;
}
@Bean(name="atelierTransactionManager")
public PlatformTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.archive.autodetection", "class, hbm");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.show_sql", "true");
return properties;
}
}
最后这是控制台输出:
... 2018-10-05 10:51:25.045信息6972--[restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat在端口上启动: 8080(http)具有上下文路径''2018-10-05 10:51:25.049 INFO 6972 --- [restartedMain] c.s.app.atelier.AtelierApplication:在8.022秒内启动AtelierApplication(JVM运行8.589) 2018-10-05 10:51:25.059信息6972--[restartedMain] o.h.j.b.internal.PersistenceXmlParser:HHH000318:找不到 类路径中的任何META-INF / persistence.xml文件
...原因:javax.persistence.PersistenceException:无持久性 名为atelierPersistenceUnit的EntityManager的提供程序...
2018-10-05 10:51:25.069信息6972--[restartedMain] ConfigServletWebServerApplicationContext:关闭 org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@44840690: 启动日期[2018年10月5日星期五10:51:17 CEST];语境的根源 层次结构2018-10-05 10:51:25.072信息6972--[restartedMain] o.s.j.e.a.AnnotationMBeanExporter:取消注册暴露于JMX的对象 关机时关闭bean 2018-10-05 10:51:25.075 INFO 6972 --- [ [restartedMain] j.LocalContainerEntityManagerFactoryBean:关闭JPA EntityManagerFactory用于持久性单元“ atelierPersistenceUnit”
我看不到出了什么问题。谢谢您的帮助。