Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导-配置文件问题的手动JPA配置_Java_Spring_Hibernate_Spring Boot_Jpa - Fatal编程技术网

Java Spring引导-配置文件问题的手动JPA配置

Java Spring引导-配置文件问题的手动JPA配置,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我的Spring Boot应用程序JPA配置有问题。我有两个配置文件——dev(h2db)和prod(PostgreSQL)。我想在没有SpringBoot“magic”的情况下手动设置JPA,所以我创建了如下所示的配置类 @Configuration @EnableTransactionManagement public class PersistenceContext { @Primary @Bean public DataSourceProperties dataSourceProper

我的Spring Boot应用程序JPA配置有问题。我有两个配置文件——dev(h2db)和prod(PostgreSQL)。我想在没有SpringBoot“magic”的情况下手动设置JPA,所以我创建了如下所示的配置类

@Configuration
@EnableTransactionManagement
public class PersistenceContext {

@Primary
@Bean
public DataSourceProperties dataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
public DataSource dataSource(DataSourceProperties properties) {
    return properties
            .initializeDataSourceBuilder()
            .type(HikariDataSource.class)
            .build();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource(dataSourceProperties()));
    em.setPackagesToScan("model");
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    return em;
}

@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
    return new JpaTransactionManager(emf);
}

}
当我想在dev概要文件中测试保存到数据库时,就会出现问题。当我执行测试时,出现以下错误:

10:37:47.951 [main] DEBUG org.hibernate.SQL - insert into Book (id, author, bookType, bookstore, new_price, old_price, title, url) values (null, ?, ?, ?, ?, ?, ?, ?)
10:37:47.955 [main] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not prepare statement [insert into Book (id, author, bookType, bookstore, new_price, old_price, title, url) values (null, ?, ?, ?, ?, ?, ?, ?)]
org.h2.jdbc.JdbcSQLException: Table "BOOK" not found; SQL statement:
insert into Book (id, author, bookType, bookstore, new_price, old_price, title, url) values (null, ?, ?, ?, ?, ?, ?, ?) [42102-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
我的application-dev.properties文件如下所示

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
spring.data.jpa.repositories.enabled=true
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

我发现
spring.jpa.properties.hibernate.hbm2ddl.auto=create drop
可能有问题,因为当我通过PersistenceContext类内的属性映射设置此属性时,它工作正常。我不知道如何通过属性文件正确设置它。提前感谢您的帮助

spring.jpa.hibernate.ddl auto=update
spring.jpa.properties.hibernate.hbm2ddl.auto=create drop
执行相同的操作。但是,由于您选择不使用Spring自动配置魔法,这些属性是无效的


因此,您必须使用JPA属性映射,并在
LocalContainerEntityManagerFactoryBean

中进行设置。您不使用Spring Boot,因此通过
应用程序设置属性。属性也不起作用。为什么你不想使用spring boot和goto来避免它呢?我只是想更好地了解JPA,以及它在没有spring提供的魔力的情况下是如何工作的