【问题标题】:JPA MySQL cant find created tableJPA MySQL 找不到创建的表
【发布时间】:2018-01-21 13:31:41
【问题描述】:

我将 JPA(Hibernate) 与 MySQL 一起使用。我在应用程序启动时创建表

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>

在我的应用程序中插入测试数据:

public class ConfigurationService {

    @Inject
    private ConfigEntryDao configEntryDao;

    private List<ConfigEntry> configEntries;


    @PostConstruct
    public void initialize() {
        configEntryDao.insert(new ConfigEntry(ConfigNamespace.xchange_test, "Hello"));
        configEntryDao.insert(new ConfigEntry(ConfigNamespace.xchange_test_subtest, "World"));
        configEntries = configEntryDao.getAll();
    }

    public void tryTestValue() {
        System.out.println("Testing data from config db table Size: "+configEntries.size()+" Values:"+ configEntries.toString());
    }

}

我得到了正确的数据,一切都很好。问题是:我找不到应该在存储数据的地方创建的表?!表名为 ConfigEntry。

服务器输出(无错误,所有预期输出正确):

    Connected to server
[2018-01-21 02:25:21,226] Artifact xChange:war exploded: Artifact is being deployed, please wait...
14:25:21,323 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0027: Starting deployment of "xChange-1.0-SNAPSHOT" (runtime-name: "xChange-1.0-SNAPSHOT.war")
14:25:22,794 INFO  [org.jboss.as.jpa] (MSC service thread 1-5) WFLYJPA0002: Read persistence.xml for mysqldb
14:25:22,926 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 62) WFLYJPA0010: Starting Persistence Unit (phase 1 of 2) Service 'xChange-1.0-SNAPSHOT#mysqldb'
14:25:22,945 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 62) HHH000204: Processing PersistenceUnitInfo [
    name: mysqldb
    ...]
14:25:22,966 INFO  [org.jboss.weld.deployer] (MSC service thread 1-7) WFLYWELD0003: Processing weld deployment xChange-1.0-SNAPSHOT.war
14:25:23,009 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 62) HHH000412: Hibernate Core {5.1.10.Final}
14:25:23,011 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 62) HHH000206: hibernate.properties not found
14:25:23,013 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 62) HHH000021: Bytecode provider name : javassist
14:25:23,016 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-7) HV000001: Hibernate Validator 5.3.5.Final
14:25:23,045 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 62) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
14:25:23,252 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-6) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.8.Final
14:25:23,395 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0005: Deploying non-JDBC-compliant driver class com.mysql.cj.jdbc.Driver (version 6.0)
14:25:23,420 INFO  [org.jboss.weld.Version] (MSC service thread 1-5) WELD-000900: 2.4.3 (Final)
14:25:23,465 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0018: Started Driver service with driver-name = xChange-1.0-SNAPSHOT.war_com.mysql.cj.jdbc.Driver_6_0
14:25:23,523 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 62) WFLYCLINF0002: Started client-mappings cache from ejb container
14:25:23,590 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 62) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'xChange-1.0-SNAPSHOT#mysqldb'
14:25:23,753 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 62) HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
14:25:23,757 WARN  [org.hibernate.dialect.H2Dialect] (ServerService Thread Pool -- 62) HHH000431: Unable to determine H2 database version, certain features may not work
14:25:23,799 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 62) Envers integration enabled? : true
14:25:24,158 INFO  [stdout] (ServerService Thread Pool -- 62) Hibernate: 
14:25:24,158 INFO  [stdout] (ServerService Thread Pool -- 62)     
14:25:24,158 INFO  [stdout] (ServerService Thread Pool -- 62)     drop table ConfigEntry if exists
14:25:24,163 INFO  [stdout] (ServerService Thread Pool -- 62) Hibernate: 
14:25:24,163 INFO  [stdout] (ServerService Thread Pool -- 62)     
14:25:24,164 INFO  [stdout] (ServerService Thread Pool -- 62)     drop sequence if exists hibernate_sequence
14:25:24,166 INFO  [stdout] (ServerService Thread Pool -- 62) Hibernate: create sequence hibernate_sequence start with 1 increment by 1
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62) Hibernate: 
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)     
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)     create table ConfigEntry (
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)         id bigint not null,
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)         namespace varchar(255) not null,
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)         value varchar(255) not null,
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)         primary key (id)
14:25:24,169 INFO  [stdout] (ServerService Thread Pool -- 62)     )
14:25:24,175 INFO  [org.hibernate.tool.schema.internal.SchemaCreatorImpl] (ServerService Thread Pool -- 62) HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@63ca3829'
14:25:24,180 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (ServerService Thread Pool -- 62) HHH000397: Using ASTQueryTranslatorFactory
14:25:24,884 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 6) Initializing Mojarra 2.2.13.SP4  for context '/xChange'
14:25:26,131 INFO  [org.primefaces.webapp.PostConstructApplicationEventListener] (ServerService Thread Pool -- 6) Running on PrimeFaces 6.1
14:25:26,421 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 6) RESTEASY002225: Deploying javax.ws.rs.core.Application: class com.bisciak.xchange.rest.RestConfig$Proxy$_$$_WeldClientProxy
14:25:26,446 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 6) WFLYUT0021: Registered web context: '/xChange' for server 'default-server'
14:25:26,475 INFO  [org.jboss.as.server] (management-handler-thread - 2) WFLYSRV0010: Deployed "xChange-1.0-SNAPSHOT" (runtime-name : "xChange-1.0-SNAPSHOT.war")
[2018-01-21 02:25:26,495] Artifact xChange:war exploded: Artifact is deployed successfully
[2018-01-21 02:25:26,496] Artifact xChange:war exploded: Deploy took 5,269 milliseconds
14:25:41,458 INFO  [stdout] (default task-2) Hibernate: 
14:25:41,458 INFO  [stdout] (default task-2)     call next value for hibernate_sequence
14:25:41,510 INFO  [stdout] (default task-2) Hibernate: 
14:25:41,510 INFO  [stdout] (default task-2)     insert 
14:25:41,510 INFO  [stdout] (default task-2)     into
14:25:41,510 INFO  [stdout] (default task-2)         ConfigEntry
14:25:41,510 INFO  [stdout] (default task-2)         (namespace, value, id) 
14:25:41,510 INFO  [stdout] (default task-2)     values
14:25:41,510 INFO  [stdout] (default task-2)         (?, ?, ?)
14:25:41,521 INFO  [stdout] (default task-2) Hibernate: 
14:25:41,521 INFO  [stdout] (default task-2)     call next value for hibernate_sequence
14:25:41,522 INFO  [stdout] (default task-2) Hibernate: 
14:25:41,522 INFO  [stdout] (default task-2)     insert 
14:25:41,522 INFO  [stdout] (default task-2)     into
14:25:41,522 INFO  [stdout] (default task-2)         ConfigEntry
14:25:41,522 INFO  [stdout] (default task-2)         (namespace, value, id) 
14:25:41,523 INFO  [stdout] (default task-2)     values
14:25:41,523 INFO  [stdout] (default task-2)         (?, ?, ?)
14:25:41,534 INFO  [stdout] (default task-2) Hibernate: 
14:25:41,534 INFO  [stdout] (default task-2)     select
14:25:41,534 INFO  [stdout] (default task-2)         configentr0_.id as id1_0_,
14:25:41,534 INFO  [stdout] (default task-2)         configentr0_.namespace as namespac2_0_,
14:25:41,534 INFO  [stdout] (default task-2)         configentr0_.value as value3_0_ 
14:25:41,534 INFO  [stdout] (default task-2)     from
14:25:41,534 INFO  [stdout] (default task-2)         ConfigEntry configentr0_
14:25:41,541 INFO  [stdout] (default task-2) Button Pressed
14:25:41,541 INFO  [stdout] (default task-2) Testing data from config db table Size: 2 Values:[ConfigEntry{id=1, namespace=xchange_test, value='Hello'}, ConfigEntry{id=2, namespace=xchange_test_subtest, value='World'}]

我尝试检查表是否已创建,但在 IntelliJ 或 MySQL Workbench 中看不到任何内容:

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="mysqldb">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xchange"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="root"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <!--Create tables automatically-->
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
        <!--<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>-->

        <!--Other sql shananigans-->
        <!--<property name="javax.persistence.schema-generation.create-source" value="script"/>-->
        <!--<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create.sql"/>-->
        <!--<property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql"/>-->
        <!--<property name="javax.persistence.schema-generation.drop-source" value="script"/>-->
        <!--<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/sql/drop.sql"/>-->

    </properties>
</persistence-unit>

任何想法在哪里可以找到创建的表?

【问题讨论】:

    标签: java mysql hibernate jpa intellij-idea


    【解决方案1】:

    通过将 "hibernate.hbm2ddl.auto"create-drop 一起赋值,当 SessionFactory 显式关闭时,数据库架构将被删除。

    create-drop 是有意义的,因为您希望拥有确切的架构(通常具有相同的数据)在每次启动时。
    它可能与某些类型的应用程序相关,并且总是在运行集成测试之前。

    要一次性创建数据库,请使用而不是create
    然后,如果您想保留架构,下次启动应用程序时,请将其更改为 validate,例如。

    【讨论】:

    • 你能告诉我在哪里可以找到创建的表吗?我应该能够在工作台上看到它们。我在后台运行应用程序,想查看创建的表,我将属性值更改为创建,仍然看不到创建的任何表,我可以看到它在日志中正确使用,我那里有大约 8 个项目,我可以全部找回,芽看不到表。
    • 我只是在测试数据库,我没有关闭应用程序并且预计数据仍然存在,我只想查看存储的数据,而应用程序在后台运行,打开已创建的表工作台
    • 这些被丢弃了,关机后你将看不到它们。为什么不使用“create”作为hbm2ddl.auto 的值?`此外,在您的日志中,h2 dialect 与 mysql 驱动程序混合在一起,并且您对 h2 dialect 有一些警告。
    • 我知道他们会被删除,即使我现在使用 create ,我仍然有同样的问题,我看不到表。创建的表在哪里?我在后台运行应用程序(未关闭),我可以看到它被使用,芽找不到它,这就是我遇到的全部问题。我希望能够在 MySQL 工作台或 razorSQL 或外部任何地方打开此表。
    • 启用日志以调试org.hibernate 包并重新启动。尝试识别日志中使用的 jdbc url。
    【解决方案2】:

    来自the documentation

    创建删除

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

    (强调我的)

    【讨论】:

    • 我没有关闭应用程序,我只想查看存储的数据,当应用程序在后台运行时,我可以看到我通过 4 次方法调用将 8 个项目存储到表中,现在我想在工作台中查看表格,我刷新了所有表格,但仍然无法在任何地方看到我的应用程序使用的表格。
    • 确保在正确的主机上连接到正确的数据库。尝试刷新表列表或断开/重新连接。尝试使用其他工具来检查您的数据库。
    猜你喜欢
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2016-04-10
    • 2012-07-14
    • 2021-01-26
    相关资源
    最近更新 更多