java - 在类路径资源中创建名称为“entityManagerFactory”的 bean 时出错,bean 的初始化失败,找不到表
问题描述
我正在为 Web 应用程序编码,但在数据库中创建字段时收到此错误。它说找不到表“用户”。我正在添加类和文件。它抛出这个错误
2020-05-05 23:04:20.539 WARN 145848 --- [ 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:/C:/Spring%20pro%20codeengine/sources/target/classes/data.sql]: insert into user values (1,'Slnmak','Codeengine@gmail.com'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "user" not found; SQL statement:
insert into user values (1,'Slnmak','Codeengine@gmail.com') [42102-200]
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-05 23:04:20.803 ERROR 145848 --- [ 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:/C:/Spring%20pro%20codeengine/sources/target/classes/data.sql]: insert into user values (1,'Slnmak','Codeengine@gmail.com'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "user" not found; SQL statement:
insert into user values (1,'Slnmak','Codeengine@gmail.com') [42102-200]
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Spring%20pro%20codeengine/sources/target/classes/data.sql]: insert into user values (1,'Slnmak','Codeengine@gmail.com'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "user" not found; SQL statement:
insert into user values (1,'Slnmak','Codeengine@gmail.com') [42102-200]
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "user" not found; SQL statement:
insert into user values (1,'Slnmak','Codeengine@gmail.com') [42102-200]
我有一个如下的类别类。类别.java
package com.example.sources.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@Table(name="category")
public class Category {
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
用户.java
package com.example.sources.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
@Table(name="user")
public class User {
@Id
private Long id;
private String name;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
并创建了一个存储库接口为 CategoryRepository.java
package com.example.sources.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.sources.model.Category;
public interface CategoryRepository extends JpaRepository<Category, Long>{
Category findByName(String name);
}
数据.sql
insert into user values (1,'Slnmak','Codeengine@gmail.com')
insert into user values (2,'John','adam@gmail.com')
insert into user values (3,'Adam','john@gmail.com')
insert into category values (1,'Travel')
insert into category values (2,'Auto Loan')
insert into category values (3,'Student Loan')
--insert into expense values (100,'New York trip','2019-06-16T17:00:00.0002' )
--insert into expense values (101,'Ford Mustang Payment','2019-06-15T15:00:00.0002' )
--insert into expense values (102,'Slnmak Trip with family','2019-06-14T15:00:00.0002' )
应用程序属性
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
pom.xml
<dependencies>
<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.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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
解决方案
应用程序无法自动创建表。用这个替换你的application.properties
文件:
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
推荐阅读
- iis - 具有域用户的IIS AppPool与域外的PC?
- python - Cannot center table on PDF-page when saving a matplotib table using backend_pdf PdfPages
- python - pyinstaller 找不到添加的文件 - Mac OS
- dictionary - 将 NLTK 树转换为字典数组
- node.js - Set Bearer Token to get authorization on Google Cloud Storage objects
- java - onActivityResult wont work after onBackPressed
- javascript - AngularJS - display a default option at the top of a dynamic select dropdown list?
- javascript - Exporting Mongoose methods in Node.js (TypeError: Cannot read property 'name' of null)
- azure - 安装在本地计算机上的 Azure VM 代理是否将数据发布到 Azure/与 azure 相关的任何服务/任何其他 URL?
- cordova - Ionic Cordova 平台添加 ios 失败,未加载库:/usr/local/opt/openssl/lib/libssl.1.0.0.dylib (LoadError)