将sql文件中的日期时间戳插入内存数据库中的h2失败。
我正在尝试在Spring应用程序启动时从.sql文件插入日期。
pom.xml如下所示:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
....
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
....
</dependencies>
属性文件如下所示:
spring:
datasource:
url: jdbc:h2:mem:my_db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
platform: h2
username: sa
password:
driverClassName: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create-drop
properties:
hibernate:
show_sql: true
use_sql_comments: true
format_sql: true
h2:
console:
enabled: true
path: /console
settings:
trace: false
web-allow-others: false
实体如下:
@Data
@Entity
@Table(
name = "class_room"
)
@NoArgsConstructor
public class Classroom
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private ZonedDateTime dateCreated = ZonedDateTime.now();
@Column(nullable = false)
@NotNull(message = "Classroom Name can not be null!")
private String classroomName;
....
}
当我使用以下命令运行应用程序时:
mvn clean install spring-boot:run
我收到以下错误:
Cannot create inner bean '(inner bean)#16f15ae9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#16f15ae9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/my_drive/app_server/target/classes/data.sql]:
插入class_room(id,date_created,...)值(1,&#39; 2018-03-15 18:47:52.69&#39;,...);嵌套异常是 org.h2.jdbc.JdbcSQLException:十六进制字符串包含非十六进制 性格:&#34; 2018-03-15 18:47:52.69&#34 ;; SQL语句:
data.sql看起来如下:
insert into class_room (id, date_created, ... ) values (1, '2018-03-15 18:47:52.69', ... )
或
insert into class_room (id, date_created, ... ) values (1, now(), ... )
和 还尝试了http://www.h2database.com/html/functions.html#current_timestamp
insert into class_room (id, date_created, ... ) values (1, CURRENT_TIMESTAMP(), ... )
我试过,从sql插入时间戳的各种方式,但不幸的是sql无法将数据放入内存数据库。
任何帮助将不胜感激!