【问题标题】:JPA: entity object is not appeared in the H2 consoleJPA:实体对象未出现在 H2 控制台中
【发布时间】:2017-12-01 19:54:15
【问题描述】:

在我的 spring 应用程序中,我有以下实体:

import lombok.Data;

@Data
@Entity

public class Profile {

    @Id
    @GeneratedValue
    Long id;

    String name;
    String address;
    String description;
    String img;

public Profile(String name, String address, String description, String img) {
        this.name = name;
        this.address = address;
        this.description = description;
        this.img = img;
    }

}

然后存储库是:

@Repository
public interface ProfileRepository extends JpaRepository<Profile,Long> {

}

这是我的pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>
        <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

所以,我希望当我转到 h2 控制台 uri 时:

http://localhost:8080/h2-console/login.do?jsessionid=ae6ba7021a23e0ebb4a4844381546e72

它显示了与对象Profile 相关的表。

问题是:没有名为 Profile 的表。

那么,为什么我没有个人资料表?

application.properties 为空

这是我连接到 H2 的方式

当我运行我的应用程序时,它会说:

[2m2017-06-29 10:07:21.106[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
[2m2017-06-29 10:07:21.557[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaExport [0;39m [2m:[0;39m HHH000227: Running hbm2ddl schema export
[2m2017-06-29 10:07:21.557[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaExport [0;39m [2m:[0;39m HHH000230: Schema export complete

看来,应用程序制作了数据库的架构。我说的对吗?

【问题讨论】:

  • 请分享您的 application.properties
  • @Salman 现在您在application.properties 中设置了一个数据库名称:dbname。因此,当您尝试从 H2 控制台 (http://localhost:8080/h2-console) 连接到 db 时,您必须使用 jdbc:h2:mem:dbname 作为“JDBC URL”。您必须从 IDE(或通过 SQL 工具)使用端口 9092 连接到 H2 DB 服务器。

标签: java spring hibernate maven spring-data-jpa


【解决方案1】:

你需要在application.properties中设置h2配置 写下这几行

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb --> whatever url you find when open console
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create

【讨论】:

    【解决方案2】:

    检查包含 @SpringBootConfiguration 注释的主类是否在包的层次结构的根目录中

    【讨论】:

      【解决方案3】:

      您也可以通过编写 schema.sql 文件来实现这一点,该文件创建表并可以通过将文件放置在 src/main/resources 位置来加载数据。有很多不同的方法可以做到这一点。我喜欢 schema.sql 文件,因为它有一些您可以查看和管理的内容。

      【讨论】:

        【解决方案4】:

        然后你打开'http://localhost:8080/h2-console'你需要指定'JDBC URL'。默认情况下(据我了解,您使用默认设置,因为您的 application.properties 为空)此值为:

        jdbc:h2:mem:testdb
        

        恕我直言 H2 控制台不是开发人员的最佳选择。您可以直接在 IDE 中使用 H2 - 请参阅 my answer。

        【讨论】:

        • 我已经用你的提示更新了我的问题,但仍然不起作用
        • @Salman 现在您在application.properties 中设置了一个数据库名称:dbname。因此,当您尝试从 H2 控制台 (http://localhost:8080/h2-console) 连接到 db 时,您必须使用 jdbc:h2:mem:dbname 作为“JDBC URL”。您必须从 IDE(或通过 SQL 工具)使用端口 9092 连接到 H2 DB 服务器。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        • 2017-12-05
        • 2023-03-07
        • 2019-03-20
        • 2020-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多