我正在将数据插入inMemory数据库,而在插入数据时却遇到问题,
使用boot,JPA,H2db在inMemory中插入数据的示例程序
创建Pojo并使用JPA注释进行注释
创建用于查询的data.sql文件。
运行应用程序。
请在屏幕截图中找到问题详细信息。
我尝试了多种方法,但仍然有相同的例外
在app.prop中配置:String url = jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
在data.sql文件中为给定表添加了@Table
如data.sql中所述,添加了用于转换的@Column
名称。
在哪里配置; DB_CLOSE_ON_EXIT=FALSE
在springboot中?
POJO
@Entity
@Table(name = "exchange_value")
public class CurrencyExchange {
@Id
private Long id;
@Column(name = "currency_from")
private String from;
@Column(name = "currency_to")
private String to;
@Column(name = "conversion_multiple")
private BigDecimal conversion;
private int port;
控制者
@Autowired
private Environment env;
@GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyExchange retriveCurrencyExchange(@PathVariable String from,@PathVariable String to)
{
CurrencyExchange currencyExchange = new CurrencyExchange(1000L, from, to, BigDecimal.valueOf(65));
currencyExchange.setPort(Integer.parseInt(env.getProperty("local.server.port")));
return currencyExchange;
}
}
应用程式
spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
data.sql file
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(1001,'USD','INR',65,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(1002,'EUR','INR',75,0);
Output: The data should be inserted into in-memory database while hitting the service.
错误原因:
对名称为'inMemoryDatabaseShutdownExecutor'的bean的销毁方法的调用失败:org.h2.jdbc.JdbcSQLNonTransientConnectionException:数据库已关闭(要在VM关闭时禁用自动关闭,请在数据库URL上添加“; DB_CLOSE_ON_EXIT = FALSE”)[90121-199 ]
org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]中创建名称为'entityManagerFactory'的bean时出错。嵌套的异常是org.springframework.jdbc.datasource.init.ScriptStatementFailedException:无法执行URL的SQL脚本语句#1 [file:/Users/naresh/Documents/workspace-sts-3.9.8.RELEASE/currency-exchange-service /target/classes/data.sql]:插入exchange_value(id,currency_from,currency_to,conversion_multiple,port)值(1001,'USD','INR',65,0);嵌套的异常是org.h2.jdbc.JdbcSQLSyntaxErrorException:未找到表“ EXCHANGE_VALUE”; SQL语句:
插入exchange_value(id,currency_from,currency_to,conversion_multiple,port)值(1001,'USD','INR',65,0)[42102-199]
org.h2.jdbc.JdbcSQLSyntaxErrorException:未找到表“ EXCHANGE_VALUE”; SQL语句:
插入exchange_value(id,currency_from,currency_to,conversion_multiple,port)值(1001,'USD','INR',65,0)[42102-199]
最佳答案
更改
String url = jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
至
spring.datasource.url: 'jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1'
在
application-properties
中另外,在插入记录之前,请确保存在表
exchange_value
(您已经编写了用于创建表的SQL)。要保持数据库打开状态,请在数据库URL上添加; DB_CLOSE_DELAY = -1。
保持内存数据库的内容与虚拟数据库一样长
机器还活着,请使用jdbc:h2:mem:test; DB_CLOSE_DELAY = -1。
H2 Database
更新
创建2个sql文件。一个创建模式,另一个创建记录
application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2
更新2
是的,Spring Boot可以为您自动创建表,以确保您具有
@Table(name = "TableName")
和spring.jpa.hibernate.ddl-auto=create
或spring.jpa.hibernate.ddl-auto=update
实体
@Entity
@Table(name="exchange_value")
public class ExchangeValueEntity {
//some fields
}
application.properties
spring.jpa.hibernate.ddl-auto=create