JPA 实体未创建表

Posted

技术标签:

【中文标题】JPA 实体未创建表【英文标题】:JPA Entity not creating table 【发布时间】:2018-04-13 20:39:26 【问题描述】:

所以我要从 JDBC 迁移到 JPA,我从 schema.sql 文件中删除了“create table” sql 语句,所以现在文件如下所示:

INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED)
VALUES(1001, 'Max', 'Mad', 0547547547, sysdate());
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED)
VALUES(1002, 'Boy', 'Johnny', 0547547547, sysdate());

我的实体 Member.class 是这样的:

@Entity
public class Member   


    @Id
    @GeneratedValue
    private int id;

    private String lastName;
    private String firstName;
    private int phoneNumber;
    private Date created;


    public Member(int id, String firstName, String lastName, int phoneNumber, Date created) 
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.created = created;
    

    public Member(String firstName, String lastName, int phoneNumber, Date created) 
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.created = created;
    


    public Member()  

    

    public long getId() 
        return id;
    
    public void setId(int userId) 
        this.id = userId;
    
    public String getFirstName() 
        return firstName;
    
    public void setFirstName(String firstName) 
        this.firstName = firstName;
    
    public String getLastName() 
        return lastName;
    
    public void setLastName(String lastName) 
        this.lastName = lastName;
    
    public long getPhoneNumber() 
        return phoneNumber;
    
    public void setPhoneNumber(int phoneNumber) 
        this.phoneNumber = phoneNumber;
    
    public Date getCreated() 
        return created;
    
    public void setCreated(Date created) 
        this.created = created;
    

    @Override
    public String toString() 
        return "Member [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber="
                + phoneNumber + ", created=" + created + "]";
    



这些是我得到的例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/apodga/javaworkspace2/springbootdb/target/classes/schema.sql]: INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()); nested exception is org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()) [42102-196]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) ~[spring-context-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M5.jar:2.0.0.M5]
    at com.recweb.springbootdb.SpringbootdbApplication.main(SpringbootdbApplication.java:29) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.0.M5.jar:2.0.0.M5]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/apodga/javaworkspace2/springbootdb/target/classes/schema.sql]: INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()); nested exception is org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()) [42102-196]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:809) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:715) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    ... 26 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/apodga/javaworkspace2/springbootdb/target/classes/schema.sql]: INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()); nested exception is org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()) [42102-196]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:225) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1011) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.postProcessAfterInitialization(DataSourceInitializerPostProcessor.java:57) ~[spring-boot-autoconfigure-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:438) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    ... 36 common frames omitted
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/apodga/javaworkspace2/springbootdb/target/classes/schema.sql]: INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()); nested exception is org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()) [42102-196]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:492) ~[spring-jdbc-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:240) ~[spring-jdbc-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48) ~[spring-jdbc-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:186) ~[spring-boot-autoconfigure-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.createSchema(DataSourceInitializer.java:102) ~[spring-boot-autoconfigure-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:64) ~[spring-boot-autoconfigure-2.0.0.M5.jar:2.0.0.M5]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1763) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700) ~[spring-beans-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    ... 49 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
INSERT INTO MEMBER (ID, LASTNAME, FIRSTNAME, PHONENUMBER, CREATED) VALUES(1001, 'Max', 'Mad', 0547547547, sysdate()) [42102-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.message.DbException.get(DbException.java:155) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.readTableOrView(Parser.java:5552) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.readTableOrView(Parser.java:5529) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.parseInsert(Parser.java:1062) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.parsePrepared(Parser.java:417) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.parse(Parser.java:321) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.parse(Parser.java:293) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.command.Parser.prepareCommand(Parser.java:258) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.engine.Session.prepareLocal(Session.java:578) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.engine.Session.prepareCommand(Session.java:519) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:176) ~[h2-1.4.196.jar:1.4.196]
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:164) ~[h2-1.4.196.jar:1.4.196]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-2.7.2.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-2.7.2.jar:na]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:471) ~[spring-jdbc-5.0.0.RELEASE.jar:5.0.0.RELEASE]
    ... 56 common frames omitted

如您所见,它显示未找到表 MEMBER。 如果我将“create table MEMBER”sql 语句带回 schema.sql 文件,则没有例外并且正在创建表,但“insert”语句没有做任何事情。我只是得到一张空桌子。 但无论如何,我不想通过 sql 语句创建表,我希望实体注释会这样做。

我在 Eclipse 上使用 Spring Boot。

谢谢!

【问题讨论】:

您需要正确配置您的项目才能使其正常工作。您可以先查看spring docs here。 @Mustafa 谢谢,我设置了 spring.datasource.initialize=false 并创建了表,但没有任何值。我错过了什么? @Mustafa 好的,没关系,真的不再需要 schema.sql 文件了。谢谢! 【参考方案1】:

在你的 application.properties 文件中添加这个休眠属性:

spring.jpa.hibernate.ddl-auto=create-drop

或者

spring.jpa.hibernate.ddl-auto=create

来自documentation:

创建删除

删除架构并在 SessionFactory 启动时重新创建它。 此外,在 SessionFactory 关闭时删除架构。

创建

将生成数据库删除,然后创建数据库。

在using hibernate.ddl-auto in production 之前你可能会三思而后行。

schema.sql 重命名为import.sqldata.sql,以便在创建will initialize the database(通过运行您的sql 文件,使用数据)之后启动它。

【讨论】:

【参考方案2】:

对我有用的事情是将这一行添加到 application.properties

spring.datasource.initialize=false

【讨论】:

【参考方案3】:

其中一个原因可能是,如果您的实体类与您的主应用程序包位于完全不同的包中。

这可能会阻止您的实体被扫描。由于没有扫描实体,因此不会将表插入到您的数据库中,这意味着您甚至不会看到任何明显的异常或错误消息。那么缺少的表可能会导致您在此处粘贴的异常。

如果是这种情况,您应该在主应用程序中使用 @EntityScan 注释。例如

@EntityScan("your.package.with.annotated.entities")
public class SomeClassInOtherPackage
    // ...

【讨论】:

【参考方案4】:

data.sql 在 spring boot 中首先被初始化。因此,它会尝试在@Entity 创建表之前插入数据... 要解决此问题,请将此代码添加到您的 application.properties。 spring.jpa.defer-datasource-initialization = true

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于JPA 实体未创建表的主要内容,如果未能解决你的问题,请参考以下文章

JPA创建实体类映射表+ 创建索引

JPA创建实体类映射表+ 创建索引

JPA/Hibernate 无法创建名为 Order 的实体

Spring-data-jpa + Hibernate 未创建预期表

MYSQL Workbench 未填充 Spring Boot JPA 实体的表数据

如何使用 JPA 注释创建连接表?