我正在尝试Spring Data。我有一个非常基本的应用程序。 部分 : 1.主要应用程序类别
@SpringBootApplication
public class CurrencyExchangeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CurrencyExchangeServiceApplication.class, args);
}
}
然后我有我的POM.XML
https://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 org.springframework.boot 弹簧启动启动器父母 2.1.13发布 com.italktocomputer.spring-boot-microservices 货币兑换服务 0.0.1-快照 货币兑换服务 Spring Boot的演示项目
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
应用程序运行正常,但是当我添加data.sql文件时:
insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1);
我收到以下错误:
2020-03-08 15:42:39.836 INFO 7053 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-03-08 15:42:39.869 INFO 7053 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=currency-exchange-service, profiles=[default], label=null, version=eaf703d5fc6fba5debfc8a23c08c6ec936082d16, state=null
2020-03-08 15:42:39.869 INFO 7053 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}]
2020-03-08 15:42:39.870 INFO 7053 --- [ restartedMain] i.s.c.CurrencyExchangeServiceApplication : No active profile set, falling back to default profiles: default
2020-03-08 15:42:40.089 INFO 7053 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-03-08 15:42:40.091 INFO 7053 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1ms. Found 0 JPA repository interfaces.
2020-03-08 15:42:40.118 INFO 7053 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=3ba16513-849a-3407-8710-797184dfd4c9
2020-03-08 15:42:40.191 INFO 7053 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8000 (http)
2020-03-08 15:42:40.192 INFO 7053 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-08 15:42:40.192 INFO 7053 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-08 15:42:40.198 INFO 7053 --- [ restartedMain] o.a.c.c.C.[Tomcat-22].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-08 15:42:40.198 INFO 7053 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 327 ms
2020-03-08 15:42:40.349 INFO 7053 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-34 - Starting...
2020-03-08 15:42:40.350 INFO 7053 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-34 - Start completed.
2020-03-08 15:42:40.355 INFO 7053 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2020-03-08 15:42:40.359 INFO 7053 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table exchange_curreny if exists
Hibernate: create table exchange_curreny (id integer not null, currency_from varchar(255), currency_to varchar(255), exchange_rate decimal(19,2), primary key (id))
2020-03-08 15:42:40.746 INFO 7053 --- [ restartedMain] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@65386fb9'
2020-03-08 15:42:40.746 INFO 7053 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-03-08 15:42:40.753 WARN 7053 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : Unable to start LiveReload server
2020-03-08 15:42:40.755 WARN 7053 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/nikku/Downloads/currency-exchange-service/target/classes/data.sql]: insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EXCHANGE_VALUE" not found; SQL statement:
insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1) [42102-200]
2020-03-08 15:42:40.959 WARN 7053 --- [ restartedMain] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
2020-03-08 15:42:40.959 INFO 7053 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-34 - Shutdown initiated...
2020-03-08 15:42:40.963 INFO 7053 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-34 - Shutdown completed.
2020-03-08 15:42:40.963 INFO 7053 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-03-08 15:42:40.970 INFO 7053 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-08 15:42:40.971 ERROR 7053 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/nikku/Downloads/currency-exchange-service/target/classes/data.sql]: insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EXCHANGE_VALUE" not found; SQL statement:
insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1) [42102-200]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at com.italktocomputer.springbootmicroservices.currencyexchangeservice.CurrencyExchangeServiceApplication.main(CurrencyExchangeServiceApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_222]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_222]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_222]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_222]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.13.RELEASE.jar:2.1.13.RELEASE]
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/nikku/Downloads/currency-exchange-service/target/classes/data.sql]: insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EXCHANGE_VALUE" not found; SQL statement:
insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1) [42102-200]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:509) ~[spring-jdbc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:239) ~[spring-jdbc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49) ~[spring-jdbc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:202) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:119) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:89) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:37) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:99) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.postProcessAfterInitialization(DataSourceInitializedPublisher.java:90) ~[spring-boot-autoconfigure-2.1.13.RELEASE.jar:2.1.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:429) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1775) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
... 20 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EXCHANGE_VALUE" not found; SQL statement:
insert into exchange_value (id,currencyFrom, currencyTo, exchangeRate) values (10000,'USD','CAD',1) [42102-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:453) ~[h2-1.4.200.jar:1.4.200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.readTableOrView(Parser.java:7628) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.readTableOrView(Parser.java:7599) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.parseInsert(Parser.java:1747) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.parsePrepared(Parser.java:954) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.parse(Parser.java:843) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.parse(Parser.java:815) ~[h2-1.4.200.jar:1.4.200]
at org.h2.command.Parser.prepareCommand(Parser.java:738) ~[h2-1.4.200.jar:1.4.200]
at org.h2.engine.Session.prepareLocal(Session.java:657) ~[h2-1.4.200.jar:1.4.200]
at org.h2.engine.Session.prepareCommand(Session.java:595) ~[h2-1.4.200.jar:1.4.200]
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235) ~[h2-1.4.200.jar:1.4.200]
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:212) ~[h2-1.4.200.jar:1.4.200]
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201) ~[h2-1.4.200.jar:1.4.200]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-3.2.0.jar:na]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:488) ~[spring-jdbc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
... 36 common frames omitted
Application.Properties
spring.application.name = currency-exchange-service
server.port = 8000
spring.jpa.show-sql = true
spring.h2.console.enabled = true
实体类:
@Entity
public class ExchangeCurreny {
ExchangeCurreny() {
}
public ExchangeCurreny(int id, String currencyFrom, String currencyTo, BigDecimal exchangeRate) {
super();
this.id = id;
this.currencyFrom = currencyFrom;
this.currencyTo = currencyTo;
this.exchangeRate = exchangeRate;
}
@Id
private int id;
private String currencyFrom, currencyTo;
private BigDecimal exchangeRate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCurrencyFrom() {
return currencyFrom;
}
public void setCurrencyFrom(String currencyFrom) {
this.currencyFrom = currencyFrom;
}
public String getCurrencyTo() {
return currencyTo;
}
public void setCurrencyTo(String currencyTo) {
this.currencyTo = currencyTo;
}
public BigDecimal getExchangeRate() {
return exchangeRate;
}
public void setExchangeRate(BigDecimal exchangeRate) {
this.exchangeRate = exchangeRate;
}
}
我不知道发生了什么。感谢您的指导。
谢谢!
答案 0 :(得分:1)
看看嵌套异常:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EXCHANGE_VALUE" not found; SQL statement:
这意味着该表不存在或至少找不到它。 我假设EXCHANGE_VALUE实际上是数据库中表的名称(仔细检查拼写错误)。
您还可以检查在Java中如何命名实体,并尝试使用@Table注释显式声明表名称。
@Table(name = "exchange_value")
public class ExchangeValue {}
答案 1 :(得分:1)
我遇到了同样的错误,但我的错误是将实体放入与 @SpringBootApplication 类不同的包中。
我的主类在“com.example.test”中,我的实体在包“com.example.test2.domain”中。
当我将我的实体放入“com.example.test.domain”时,一切都恢复了正常。
答案 2 :(得分:0)
我已关注https://www.baeldung.com/spring-boot-data-sql-and-schema-sql
create table exchange_value (id bigint not null, fromvalue varchar(255), multiplication_factor double not null, to varchar(255), primary key (id));
-> Schema.sqlinsert into exchange_value (id,fromvalue,multiplication_factor,to) values (1,'USD',889,'INR');
--> 数据.sqlspring.jpa.hibernate.ddl-auto=none
`