JPA MySQL无法找到创建的表

时间:2018-01-21 13:31:41

标签: java mysql hibernate jpa intellij-idea

我在MySQL中使用JPA(Hibernate)。我在应用程序启动时创建表

<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中看到任何内容: enter image description here enter image description here

这是我的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>

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

2 个答案:

答案 0 :(得分:1)

来自the documentation

  

创建降

     

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

(强调我的)

答案 1 :(得分:1)

通过使用"hibernate.hbm2ddl.auto"create-drop进行评估,将明确关闭SessionFactory时将删除数据库架构。
create-drop有意义,因为您希望在每次启动时都拥有准确的架构(通常具有相同的数据) 它可能与某些类型的应用程序相关,并且始终在运行集成测试之前。

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