【问题标题】:JBoss hibernate error in inserting data插入数据时JBoss休眠错误
【发布时间】:2016-03-10 23:08:00
【问题描述】:

我配置了一个数据源并在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>

【问题讨论】:

    标签: mysql hibernate wildfly mariadb


    【解决方案1】:

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

    如果您暂时不关心数据的持久化,而只想测试您的应用程序/架构,您也可以使用 Wildfly 包含的 ExampleDS,它使用 H2 内存数据库。

    【讨论】:

    • 我想建立自己的数据库。 ExampleDS 工作正常。我创建了一个空数据库 BombwordDS,但仍然收到相同的错误。现在摆脱“未选择数据库”问题?
    • 你能分享你的数据源配置吗?
    • Remi,请看我在standalone.xml中的数据源配置。当我在 wildfly 管理控制台中进行测试时,数据源连接良好。
    • 我错过了 URL 连接中的 BombwordDS。更正,它的工作原理。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2011-06-20
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多