H2 控制台看不到 JAVA 创建的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24116962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
H2 Console Cant see tables created by JAVA
提问by Dunken
I have downloaded the H2 console from http://www.h2database.com/html/download.html
and I have configured the URL in my jdbc.properties
file
to jdbc:h2:c:/data/Messaging
.
我已从http://www.h2database.com/html/download.html下载了 H2 控制台,
并将jdbc.properties
文件中的 URL 配置
为jdbc:h2:c:/data/Messaging
.
I am using the same URL in the file to connect to the database but I cannot see the tables;
I can only see the Information Schema and when I try to select * from tables
in it I cannot see the tables neither.
我在文件中使用相同的 URL 连接到数据库,但我看不到表;我只能看到信息架构,当我尝试使用select * from tables
它时,我也看不到表格。
Does anybody have any idea what could be wrong?
有没有人知道可能有什么问题?
回答by Framus
Had the same Problem.
有同样的问题。
This solved it for me: Why is my embedded h2 program writing to a .mv.db file
这为我解决了这个问题: 为什么我的嵌入式 h2 程序写入 .mv.db 文件
Just added ;MV_STORE=FALSE
and ;MVCC=FALSE
to the jdbc url and everything worked just fine.
只需添加;MV_STORE=FALSE
并;MVCC=FALSE
到JDBC URL,一切工作就好了。
回答by Stephan
回答by Dan Barowy
Based on your question, it doesn't look like you fell victim to this particular pitfall, but this thread ended up helping me nail down the issue, so I am recording the solution here for posterity since it may help others with the same problem.
根据您的问题,您似乎并没有成为这个特定陷阱的受害者,但是这个帖子最终帮助我确定了这个问题,所以我在这里记录解决方案以供后代使用,因为它可能会帮助其他人解决同样的问题。
I also found that when I tried to open my database with the H2 console that I got what appeared to be a blank H2 database (basically, just an INFORMATION_SCHEMA table). While double-checking that I got the name of the DB correct (mydb.mv.db
), I discovered that the H2 console had created a second database file, mydb.mv.db.mv.db
. Odd.
我还发现,当我尝试使用 H2 控制台打开我的数据库时,我得到了一个看似空白的 H2 数据库(基本上,只是一个 INFORMATION_SCHEMA 表)。在仔细检查数据库名称是否正确 ( mydb.mv.db
) 时,我发现 H2 控制台创建了第二个数据库文件mydb.mv.db.mv.db
. 奇怪的。
It turns out that the H2 Console expects you to omit the suffix .mv.db
from the filename. Since I hadn't, it was looking for mydb.mv.db.mv.db
. Changing the JDBC string to jdbc:h2:mydb
solved the problem and I could then open the file from the H2 Console.
事实证明,H2 控制台希望您.mv.db
从文件名中省略后缀。因为我没有,它正在寻找mydb.mv.db.mv.db
. 更改 JDBC 字符串以jdbc:h2:mydb
解决问题,然后我可以从 H2 控制台打开该文件。
回答by Krishnendu
I have used the below and I see my table get created.
我使用了下面的内容,我看到我的表被创建了。
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
spring.h2.console.enabled=true
spring.h2.console.path=/h2console
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
回答by HA S
This is how you enable memory enable database using h2 module. You need to ensure the following things
这就是使用 h2 模块启用内存启用数据库的方法。您需要确保以下几点
- You had class that has @Entity annotations.
- you need to enable the following in application.properties file
spring.h2.console.enabled=true
- Run Spring Boot and enter the following URL
localhost:8080/h2-console
- This will show a connection screen. Enter the following changes in the
JDBC URL:
->jdbc:h2:mem:testdb
5.Hit the connection button
- 你有一个带有@Entity 注释的类。
- 您需要在 application.properties 文件中启用以下内容
spring.h2.console.enabled=true
- 运行 Spring Boot 并输入以下 URL
localhost:8080/h2-console
- 这将显示一个连接屏幕。在
JDBC URL:
->jdbc:h2:mem:testdb
5.Hit the connection 按钮中输入以下更改
Salam
萨拉姆
回答by MattC
One tricky thing is that the H2 console will not give you an error if you try to connect to a JDBC URL that doesn't exist. It will instead create a new database at that URL! To connect to the in memory DB, use this JDBC URL (http://localhost:8080/h2-consoleis the default console):
一件棘手的事情是,如果您尝试连接到一个不存在的 JDBC URL,H2 控制台不会给您一个错误。相反,它将在该 URL 上创建一个新数据库!要连接到内存数据库,请使用此 JDBC URL(http://localhost:8080/h2-console是默认控制台):
jdbc:h2:mem:testdb
If you were to enter something like jdbc:h2:~/test then a test.mv file would be created under your home directory. But your application would still be using the in memory database.
如果您要输入类似 jdbc:h2:~/test 的内容,那么将在您的主目录下创建一个 test.mv 文件。但是您的应用程序仍将使用内存数据库。
The console is available if you have the h2 dependency in your pom, and also the spring developer tools dependency. If you don't have the tools dependency, then you can also see it by having the h2 dependency and adding the following to your application.properties file:
如果您的 pom 中具有 h2 依赖项以及 spring 开发人员工具依赖项,则控制台可用。如果您没有工具依赖项,那么您也可以通过拥有 h2 依赖项并将以下内容添加到您的 application.properties 文件中来查看它:
spring.h2.console.enabled=true #not needed if you have spring-boot-devtools dependency
If you want the db as a file, and not in memory, add the following to applications.properties:
如果您希望将 db 作为文件而不是在内存中,请将以下内容添加到 applications.properties:
spring.datasource.url=jdbc:h2:~/test_db #You will see the file in your home directory.
H2 isn't meant for persisted data, but if you want to persist for testing purposes, then add:
H2 不适用于持久化数据,但如果您想为了测试目的而持久化,请添加:
spring.jpa.hibernate.ddl-auto = update
Then start up the app, and at the console, use this JDBC URL:
然后启动应用程序,并在控制台中使用此 JDBC URL:
jdbc:h2:~/test_db
In case you were wondering, I only have 1 entry in application.properties (for the database file) and here are my dependencies:
如果您想知道,我在 application.properties(用于数据库文件)中只有 1 个条目,这是我的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
回答by VRadhe
Add Annotation @EntityScan("packageName") in main class
在主类中添加注解@EntityScan("packageName")