【发布时间】:2019-07-25 06:48:05
【问题描述】:
我在 inMemory 数据库中插入数据,在插入数据时遇到问题,
使用 boot、JPA、H2db 在 inMemory 中插入数据的示例程序
创建 Pojo 并使用 JPA 注释进行注释
为查询创建 data.sql 文件。
运行应用程序。 请在屏幕截图中找到问题详细信息。
我尝试了很多方法,但仍然是同样的例外
在 app.prop 中配置:
String url = jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE在 data.sql 文件中为给定表添加了
@Table添加了
@Column用于转换的名称,如 data.sql 中所述。
在哪里配置; 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;
}
}
app.prop
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 上调用 destroy 方法失败:org.h2.jdbc.JdbcSQLNonTransientConnectionException:数据库已关闭(要在 VM 关闭时禁用自动关闭,请将“;DB_CLOSE_ON_EXIT=FALSE”添加到 db URL)[90121-199 ] org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义名称为“entityManagerFactory”的bean创建错误:bean初始化失败;嵌套异常是 org.springframework.jdbc.datasource.init.ScriptStatementFailedException:无法执行 URL [file:/Users/naresh/Documents/workspace-sts-3.9.8.RELEASE/currency-exchange-service 的 SQL 脚本语句#1 /target/classes/data.sql]:插入 exchange_value(id,currency_from,currency_to,conversion_multiple,port) values(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]
【问题讨论】:
-
错误提示
EXCHANGE_VALUE表不存在
标签: java database spring spring-boot h2