插入数据时{​​JB} hibernate错误

时间:2015-12-06 07:21:02

标签: mysql hibernate wildfly mariadb

我配置了一个数据源并在persistence.xml中使用它:

<persistence-unit name="myds">
      <jta-data-source>java:jboss/datasources/BBDS</jta-data-source>
      <properties>         
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>

我测试了它,它显示连接成功:

  

http://localhost:9990/console

在我的Web应用程序中,我有一个import.sql用于初始加载到数据库:

insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212') 

我正在使用Wildfly服务器,我尝试启动我的应用程序,但收到此错误消息。它似乎无法加载初始会员记录。

01:57:49,830 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) JBAS010403: Deploying JDBC-compliant driver class org.hsqldb.jdbc.JDBCDriver (version 2.0)
01:57:49,831 INFO  [org.jboss.weld.deployer] (MSC service thread 1-8) JBAS016005: Starting Services for CDI deployment: bword.war
01:57:49,854 INFO  [org.jboss.weld.Version] (MSC service thread 1-8) WELD-000900: 2.2.6 (Final)
01:57:49,872 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-15) JBAS010417: Started Driver service with driver-name = bword.war_org.hsqldb.jdbc.JDBCDriver_2_0
01:57:49,872 INFO  [org.jboss.weld.deployer] (MSC service thread 1-10) JBAS016008: Starting weld service for deployment bword.war
01:57:49,970 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 50) JBAS011409: Starting Persistence Unit (phase 2 of 2) Service 'bword.war#myds'
01:57:50,022 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 50) HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
01:57:50,109 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 50) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
01:57:50,196 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 50) HHH000397: Using ASTQueryTranslatorFactory
01:57:50,410 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000227: Running hbm2ddl schema export
01:57:50,416 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: drop table if exists Member
01:57:50,416 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,417 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: drop table if exists hibernate_sequence
01:57:50,417 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,417 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: create table Member (id bigint not null, email varchar(255) not null, name varchar(25) not null, phone_number varchar(12) not null, primary key (id))
01:57:50,417 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: alter table Member add constraint UK_9qv6yhjqm8iafto8qk452gx8h  unique (email)
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: create table hibernate_sequence ( next_val bigint )
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000389: Unsuccessful: insert into hibernate_sequence values ( 1 )
01:57:50,418 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,419 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000388: Unsuccessful: insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212')
01:57:50,419 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) No database selected
01:57:50,419 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000230: Schema export complete

我有一个具体的问题:

1)数据源名称为“BBDS”。在运行应用程序之前,我应该在MySql(MariaDB)中创建数据库名称“BBDS”吗?我认为由于

,数据库和表格将自动创建和销毁

2)“没有选择数据库”,在哪里指定数据库?

编辑:

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
            <datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <datasource jndi-name="java:/jboss/datasources/BBDS" pool-name="BBDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mariadb://localhost:3306</connection-url>
                    <driver-class>org.mariadb.jdbc.Driver</driver-class>
                    <driver>mariadb-java-client-1.3.2.jar</driver>
                    <security>
                        <user-name>testUser</user-name>
                        <password>testPass</password>
                    </security>
                </datasource>
                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>

1 个答案:

答案 0 :(得分:1)

使用此类配置不会自动为您创建数据库。在部署此应用程序之前,必须在wildfly中配置BombwordDS数据源,它应该引用现有数据库。

如果你现在不关心保存数据,只想测试你的应用程序/架构,你也可以使用Wildfly包含的使用H2内存数据库的ExampleDS。