运行服务器时,Hibernate 不创建表

Posted

技术标签:

【中文标题】运行服务器时,Hibernate 不创建表【英文标题】:Hibernate is not creating tables when the server is run 【发布时间】:2015-07-17 03:39:45 【问题描述】:

我是 Java EE 的新手,我正在制作一个动态 Web 项目,并且正在使用 Hibernate 和 mysql。我已经创建了我的实体,当我运行 Wildfly 时它会打印此堆栈。

14:19:52,791 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 50) HHH000412: Hibernate Core 4.3.7.Final
14:19:52,798 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 50) HHH000206: hibernate.properties not found
14:19:52,804 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 50) HHH000021: Bytecode provider name : javassist
14:19:53,453 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016002: Processing weld deployment TiendaVirtual2.war
14:19:54,043 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-2) HV000001: Hibernate Validator 5.1.3.Final
14:19:55,456 INFO  [org.jboss.weld.deployer] (MSC service thread 1-7) JBAS016005: Starting Services for CDI deployment: TiendaVirtual2.war
14:19:55,622 INFO  [org.jboss.weld.Version] (MSC service thread 1-7) WELD-000900: 2.2.6 (Final)
14:19:55,927 INFO  [org.jboss.weld.deployer] (MSC service thread 1-6) JBAS016008: Starting weld service for deployment TiendaVirtual2.war
14:19:57,096 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 50) JBAS011409: Starting Persistence Unit (phase 2 of 2) Service 'TiendaVirtual2.war#hibernate_jpa'
14:19:57,650 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 50) HCANN000001: Hibernate Commons Annotations 4.0.4.Final
14:19:59,059 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 50) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
14:20:00,184 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 50) HHH000397: Using ASTQueryTranslatorFactory
14:20:02,361 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000227: Running hbm2ddl schema export
14:20:02,391 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table Compra drop foreign key FK_qvm4tg17h4e1q12ycgu7wx4ig
14:20:02,395 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: alter table Compra drop foreign key FK_qvm4tg17h4e1q12ycgu7wx4ig
14:20:02,396 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) Tabla "COMPRA" no encontrada
Table "COMPRA" not found; SQL statement:
alter table Compra drop foreign key FK_qvm4tg17h4e1q12ycgu7wx4ig [42102-173]
14:20:02,397 INFO  [stdout] (ServerService Thread Pool -- 50)               Hibernate: alter table Compra_Producto drop foreign key     FK_qdqcipjwej5f1v6w0hureucb0
14:20:02,398 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: alter table Compra_Producto drop foreign key FK_qdqcipjwej5f1v6w0hureucb0
14:20:02,400 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) Tabla "COMPRA_PRODUCTO" no encontrada
Table "COMPRA_PRODUCTO" not found; SQL statement:
alter table Compra_Producto drop foreign key FK_qdqcipjwej5f1v6w0hureucb0 [42102-173]
14:20:02,403 INFO  [stdout] (ServerService Thread Pool -- 50)    Hibernate: alter table Compra_Producto drop foreign key FK_fvttmqakhx55owd7alp2otv7m
14:20:02,419 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: alter table Compra_Producto drop foreign key FK_fvttmqakhx55owd7alp2otv7m
14:20:02,420 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) Tabla "COMPRA_PRODUCTO" no encontrada
Table "COMPRA_PRODUCTO" not found; SQL statement:
alter table Compra_Producto drop foreign key FK_fvttmqakhx55owd7alp2otv7m [42102-173]
14:20:02,424 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table usuario drop foreign key FK_j0ef5x05bqaxdtgegawb7qjua
14:20:02,438 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: alter table usuario drop foreign key FK_j0ef5x05bqaxdtgegawb7qjua
14:20:02,439 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) Tabla "USUARIO" no encontrada
Table "USUARIO" not found; SQL statement:
alter table usuario drop foreign key FK_j0ef5x05bqaxdtgegawb7qjua [42102-173]
14:20:02,441 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists Compra
14:20:02,449 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists Compra_Producto
14:20:02,451 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists Producto
14:20:02,463 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists tarjeta_virtual
14:20:02,466 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists usuario
14:20:02,468 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: drop table if exists hibernate_sequence
14:20:02,469 INFO  [stdout] (ServerService Thread Pool -- 50)    Hibernate: create table Compra (id bigint not null, date date, valor double  precision not null, usuario_id_usuario bigint, primary key (id))
14:20:02,472 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: create table Compra_Producto (Compra_id bigint not null, productos_id bigint not null)
14:20:02,475 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: create table Producto (id bigint not null, nombre varchar(255), precio double precision not null, stock integer not null, primary key (id))
14:20:02,485 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: create table tarjeta_virtual (numero varchar(255) not null, saldo double precision, primary key (numero))
14:20:02,491 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: create table usuario (id_usuario bigint not null, nombre varchar(255), password varchar(255), usuario varchar(255), tarjeta_numero varchar(255), primary key (id_usuario))
14:20:02,496 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table Compra_Producto add constraint UK_qdqcipjwej5f1v6w0hureucb0  unique (productos_id)
14:20:02,504 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table Compra add constraint FK_qvm4tg17h4e1q12ycgu7wx4ig foreign key (usuario_id_usuario) references usuario (id_usuario)
14:20:02,538 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table Compra_Producto add constraint FK_qdqcipjwej5f1v6w0hureucb0 foreign key (productos_id) references Producto (id)
14:20:02,544 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table Compra_Producto add constraint FK_fvttmqakhx55owd7alp2otv7m foreign key (Compra_id) references Compra (id)
14:20:02,561 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: alter table usuario add constraint FK_j0ef5x05bqaxdtgegawb7qjua foreign key (tarjeta_numero) references tarjeta_virtual (numero)
14:20:02,576 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: create table hibernate_sequence ( next_val bigint )
14:20:02,577 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: insert into hibernate_sequence values ( 1 )
14:20:02,582 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000230: Schema export complete
14:20:02,584 WARN  [org.hibernate.internal.SessionFactoryImpl] (ServerService Thread Pool -- 50) HHH000008: JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()
14:20:08,465 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-6) Inicializando Mojarra 2.2.8-jbossorg-1 20140822-1131 para el contexto '/TiendaVirtual2'
14:20:11,483 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017534: Registered web context: /TiendaVirtual2
14:20:11,829 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "TiendaVirtual2.war" (runtime-name : "TiendaVirtual2.war")
14:20:11,940 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
14:20:11,943 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
14:20:11,945 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874:      WildFly 8.2.0.Final "Tweek" started in 41461ms - Started 279 of 333              services (92 services are lazy, passive or on-demand)

看起来它试图找到要删除它们的表,然后创建它们,但是当我进入数据库时​​,没有创建表。

这是我的persistence.xml 文件

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">

<persistence-unit name="hibernate_jpa" transaction-type="RESOURCE_LOCAL">
    <description>
        Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
    </description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.aplicaciones.tiendavirtual.model.Usuario</class>
    <class>com.aplicaciones.tiendavirtual.model.Compra</class>
    <class>com.aplicaciones.tiendavirtual.model.TarjetaVirtual</class>
    <class>com.aplicaciones.tiendavirtual.model.Producto</class>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tienda_virtual" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="pwd" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="create" />

    </properties>

</persistence-unit>

如果您需要其他东西,请不要犹豫,我非常感谢您能给我的任何帮助。我已经坚持了好几个小时了。

【问题讨论】:

我会从代码 sn-p 中删除密码。 Hibernate 结合 JavaEE 和应用服务器通常使用 JTA 数据源并注意 RESOURCE_LOCAL。当前休眠版本已弃用您的提供程序字符串。所以我会考虑使用在应用服务器上配置的 JTA 数据源。 您好,感谢您抽出宝贵时间回答。您提到提供程序已被弃用,那么我应该在哪里定义它?再次感谢。 位置正确,但包已更改。新的是 org.hibernate.jpa.HibernatePersistenceProviderorg.hibernate.jpa.HibernatePersistenceProvider 我认为从 Hibernate 4 开始,但我不是 100% 确定,但我认为这不是关键问题。 【参考方案1】:

正如 mh-dev 所说,我需要使用 JTA 数据源,而且我必须将 JDBC 驱动程序配置为能够连接到数据库的模块。

【讨论】:

以上是关于运行服务器时,Hibernate 不创建表的主要内容,如果未能解决你的问题,请参考以下文章

学习总结-hibernate设计表

如何在运行时在 Hibernate 中创建数据库?

休眠表不存在错误

Spring Boot with Hibernate:在已经存在时继续创建数据库

Hibernate 在启动时进行 DDL 验证时不使用 @Table。使用 Flyway 和 TestContainers

hibernate 项目启动时总会自动创建表