如何解决找不到带有H2投掷表的Spring MVC
我知道这种问题发生过很多次,但是我仍然缺少一些东西。我正在用经典的Spring MVC(不是Boot!),带有Hibernate和不幸的H2的JPA构建一些简单的网上商店应用程序:)
这是我的application.properties
:
################### DataSource Configuration ##########################
spring.datasource.url=jdbc:h2:mem:webshop;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.username=webshopdba
spring.datasource.password=123
spring.datasource.initialize=true
################### JPA ###################
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
################### H2 ###################
spring.h2.console.path=/h2
spring.h2.console.enabled=true
################### Hibernate ###################
logging.level.org.hibernate.SQL=INFO
hibernate.show-sql=true
这是一个示例模型类:
package com.webshop.model;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Entity
@Data
@Table(name = "PROGRAM")
public class Program {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "PROGRAM_NAME")
private String programName;
@Column(name = "DESCRIPTION")
private String description;
}
这是持久性配置:
@Configuration
@EnableSpringDataWebSupport
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = "com.webshop.repositories")
@EnableJpaRepositories(basePackages = "com.webshop.repositories")
public class PersistenceConfig {
private final Environment env;
@Autowired
public PersistenceConfig(Environment env) {
this.env = env;
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("webshop")
.build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.show-sql",env.getProperty("hibernate.show-sql"));
jpaProperties.put("hibernate.dialect",env.getProperty("spring.jpa.properties.hibernate.dialect"));
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setPackagesToScan("com.webshop");
factory.setJpaVendorAdapter(vendorAdapter);
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public DataSourceInitializer dataSourceInitializer() {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("/schema-h2.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource());
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
这是schema-h2.sql
:
CREATE SCHEMA IF NOT EXISTS webshop;
SET SCHEMA webshop;
CREATE USER IF NOT EXISTS WEBSHOPDBA PASSWORD '123' ADMIN;
CREATE TABLE IF NOT EXISTS PROGRAM
(
ID BIGINT PRIMARY KEY AUTO_INCREMENT,PROGRAM_NAME VARCHAR(255),DESCRIPTION VARCHAR(255)
);
INSERT INTO PROGRAM
(PROGRAM_NAME,DESCRIPTION)
VALUES ('Example software','Example')
现在,应用程序正确启动,读取schema-h2.sql,加载它并准备工作,但是如果我执行与持久性相关的任何操作,它将抛出
SqlExceptionHelper:142 - Table "PROGRAM" not found; SQL statement:
insert into PROGRAM (PROGRAM_NAME,DESCRIPTION) values (null,?,?) [42102-196]
尽管我可以使用/h2-console
路径进入H2,在其中存在我的模式,表和一个示例插入(使用SQL中定义的用户)。我还可以在tomcat日志中看到我的查询已成功执行。这里可能是什么问题?我觉得当尝试执行任何持久性操作时,我的代码创建了一个新的数据库实例,而前一个实例丢失了,但是根据H2文档,DB_CLOSE_DELAY=-1
应该可以防止这种情况。
到目前为止我已经尝试过:
- 将hibernate ddl-auto或hibernate.hbm2ddl.auto设置为创建/创建-删除/更新/无-没有区别,
spring.jpa.hibernate.ddl-auto=create
- 将我的SQL文件分为模式/数据,导入,仅模式,也没有区别,
- 明确声明
spring.datasource.initialization-mode=embedded
无效 - 将初始化架构添加到连接字符串,
- 使用DriverManagerDataSource声明数据源
我的想法慢慢消失了,有人可以建议我在这里做错了什么吗?帮助我StackOverflow,这是我唯一的希望!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。