java - 服务器运行时 Hibernate 不创建表

标签 java mysql hibernate jakarta-ee wildfly-8

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

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

最佳答案

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

关于java - 服务器运行时 Hibernate 不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086270/

相关文章:

java - 从人类可读的时区字符串中获取时区 ID

MySQL 查询/子查询问题

php - 在保持相同连接的同时在同一类中使用不同的 mysql 用户?

hibernate - Spring-data JPA 存储库通过在结果中丢失空值来排序

java - Python 中的 ord 和 chr 等方法

Java序列化未实现 `Serializable`的字段

mysql - Scala、Slick 和 1970 年之前的日期

hibernate - Grails 2.4.4:createCriteria:如何按顺序使用日期和整数之和?

来自数据库的 hibernate 映射中的 Java 日期(时区问题和夏令时)

java - onAttach() or onActivityCreated(),哪个是读取activity最好的地方?