通过JDBC语句插入hibernate_sequence值(1)“:org.hibernate.tool.schema.spi.CommandAcceptanceException:执行DDL时出错”

时间:2018-12-24 09:17:39

标签: hibernate jpa wildfly datasource mysql-8.0

wildfly版本:wildfly-15 Hibernate版本:Hibernate-5

  <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" spy="true" tracking="true" enlistment-trace="true" statistics-enabled="true">
                    <connection-url>jdbc:mysql://localhost:3306/sample</connection-url>
                    <driver-class>com.mysql.cj.jdbc.Driver</driver-class>
                    <datasource-class>com.mysql.cj.jdbc.MysqlDataSource</datasource-class>
                    <connection-property name="useSSL">
                        false
                    </connection-property>
                    <connection-property name="characterEncoding">
                        UTF-8
                    </connection-property>
                    <connection-property name="serverTimezone">
                        GMT+8
                    </connection-property>
                    <driver>mysql</driver>
                    <security>
                        <user-name>sample</user-name>
                        <password>sample</password>
                    </security>
                    <validation>
                        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                        <background-validation>true</background-validation>
                        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                    </validation>
                    <statement>
                        <share-prepared-statements>true</share-prepared-statements>
                    </statement>
                </datasource>

实体:

@Entity
@Table(schema = "sample", name="person")
@Data
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * primary key and increment strategy
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer personId;

    /**
     * Field Constraint: no-null、length=32
     */
    @Column(nullable=false,length=32)
    private String name;

    @Column(nullable=false)
    private Integer age;

    @Temporal(value=TemporalType.DATE)
    private Date birthday;
}

persitence.xml:

    <persistence-unit name="persistenceUnit">
     <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
     <properties>
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.format_sql" value="true"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
     </properties>
</persistence-unit>

错误信息:

18:25:26,417 WARN  [org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl] (ServerService Thread Pool -- 11) GenerationTarget encountered exception accepting command : Error executing DDL "
    insert into hibernate_sequence values ( 1 )" via JDBC Statement: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
    insert into hibernate_sequence values ( 1 )" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939)
    at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:170)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:128)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:649)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:212)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
    at java.lang.Thread.run(Thread.java:748)
    at org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: java.sql.SQLException: No database selected

问题: 当我配置wildfly mysql数据源时,当我登录wildfly管理Web ui时,可以成功测试数据源,但是当我将带有jpa configure的ejb项目打包到jar中时,会出现错误信息,我该如何解决?谢谢

1 个答案:

答案 0 :(得分:0)

从所附的屏幕截图中可以清楚地看出,错误是

  

未选择数据库

显然,当您的应用程序不知道要使用哪个数据库时,就会发生这种情况。要解决此问题,您应该从以下位置更改数据库驱动程序URL:

"URL"="jdbc:mysql://localhost:3306/" 

"URL"="jdbc:mysql://localhost:3306/databasename"

因此,请检查您的数据库配置文件并添加数据库名称,如下所示。

有关更多信息,请检查this

,并避免使用图片,而应使用代码。