有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我对JavaEE非常陌生,我正在做一个动态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>

如果你还需要别的东西,请不要犹豫,我很感激你能给我的任何帮助。我已经被困在这个问题上好几个小时了


共 (1) 个答案

  1. # 1 楼答案

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