休眠升级后表丢失

时间:2016-09-29 12:58:45

标签: hibernate wildfly jpa-2.1 wildfly-10 hibernate-5.x

我们刚刚从WildFly 9升级到10,因此也从Hibernate 4升级到5。 我们的应用程序使用从具有生成的id字段的抽象超级实体派生的实体。 超级课程看起来像这样:

@MappedSuperclass
@Access(AccessType.FIELD)
@SqlResultSetMapping(name = EntityId.rsmId,
    columns = { @ColumnResult(name = "id", type = Long.class) })
public abstract class EntityId
{
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "generator1")
    @TableGenerator(name = "generator1",
        pkColumnValue = "g1",
        initialValue = 0, allocationSize = 1000,
        schema = "schema2", table = "sysidgenerator", // MySQL
        pkColumnName = "gen_key", valueColumnName = "gen_val")
    @Column(name = "id")
    @Id
    private Long id;

[...]
}

and our datasource is configured like this:

<xa-datasource jndi-name="java:jboss/datasources/MTDS" pool-name="MTDS" enabled="true" use-ccm="false">
    <xa-datasource-property name="URL">
        jdbc:mysql://server:3306/schema
    </xa-datasource-property>
    <driver>com.mysql</driver>
    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
    <xa-pool>
        <min-pool-size>3</min-pool-size>
        <max-pool-size>40</max-pool-size>
        <is-same-rm-override>false</is-same-rm-override>
        <interleaving>false</interleaving>
        <pad-xid>false</pad-xid>
        <wrap-xa-resource>false</wrap-xa-resource>
    </xa-pool>
    <security>
        <user-name>user</user-name>
        <password>password</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <validate-on-match>false</validate-on-match>
        <background-validation>true</background-validation>
        <background-validation-millis>15000</background-validation-millis>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
    <statement>
        <prepared-statement-cache-size>0</prepared-statement-cache-size>
        <share-prepared-statements>false</share-prepared-statements>
    </statement>
</xa-datasource>

使用Hibernate 4一切都很完美。升级后,我们收到以下错误:

ERROR [x.y.z] (default task-13) Caught exception.: javax.ejb.EJBTransactionRolledbackException: org.hibernate.HibernateException: Could not apply work
Caused by: org.hibernate.HibernateException: Unable to perform isolated work
    at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doTheWork(JtaIsolationDelegate.java:139)
    at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doTheWorkInNewTransaction(JtaIsolationDelegate.java:100)
    ... 253 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'schema.sysidgenerator' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
    at com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper.executeQuery(PreparedStatementWrapper.java:844)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:504)
    at org.hibernate.id.enhanced.TableGenerator.executeQuery(TableGenerator.java:639)
    at org.hibernate.id.enhanced.TableGenerator.access$400(TableGenerator.java:127)
    at org.hibernate.id.enhanced.TableGenerator$1$1.execute(TableGenerator.java:541)
    at org.hibernate.id.enhanced.TableGenerator$1$1.execute(TableGenerator.java:531)
    at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doTheWork(JtaIsolationDelegate.java:133)
    ... 254 more

出于某种原因,Hibernate现在正在寻找表格&gt; schema.sysidgenerator&#39;而不是&#39; schema2.sysidgenerator&#39;就像它在@TableGenerator中配置一样。

我们需要做些什么来解决这个问题? 建议表示赞赏。

此致

1 个答案:

答案 0 :(得分:2)

你应该试试&#34;目录&#34;而不是&#34; schema&#34;,因为MySQL不支持模式。

有关详细信息,请查看HHH-9714

  

MySQL / MariaDB不支持模式,它们支持目录。我知道这很令人困惑,因为命令行允许你说CREATE SCHEMA,但是他们的文档很清楚这只是一个&#34;同义词&#34;对于CREATE DATABASE。 JDBC驱动程序仅支持目录。事实上,他们的DatabaseMetaData对所有{{#supportsSchemasIn?}方法返回false,对{{#supportsCatalogsIn?)返回true。

     

这可能是旧版Hibernate的变化。 TBH我们对目录和模式的支持(特别是在模式工具中)是可疑的。