【问题标题】:Hibernate auto table generation from domain class is not working in Grails with MySQL来自域类的 Hibernate 自动表生成在 Grails 和 MySQL 中不起作用
【发布时间】:2021-10-30 22:21:53
【问题描述】:

所有域类在 MySQL 中都被映射为表,而不是一个名为 USER 的域类(映射中表的名称更改为 db_user 以避免关键字违规)如下所示@ 987654321@:

并且应用程序在启动时一直显示错误:

原因:java.sql.SQLSyntaxErrorException:表 'umm.db_user' 不存在

堆栈版本:

| Grails 版本:3.2.3

| Groovy 版本:2.4.7

| JVM版本:1.8.0_242

|摇篮:3.0

| MySQL:8.0.26-集群

|休眠:5.1.1.Final

application.groovy

application.yml

日志:

2021-09-02 02:43:09.722 ERROR 26370 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table 'umm.db_user' doesn't exist
2021-09-02 02:43:09.783 ERROR 26370 --- [           main] grails.app.init.umm1.BootStrap           : Exception occured in bootstrap: 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet 
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2122)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1905)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
    at org.hibernate.loader.Loader.doQuery(Loader.java:925)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
    at org.grails.orm.hibernate.query.AbstractHibernateQuery.singleResultViaListCall(AbstractHibernateQuery.java:785)
    at org.grails.orm.hibernate.query.AbstractHibernateQuery.singleResult(AbstractHibernateQuery.java:775)
Caused by: java.sql.SQLSyntaxErrorException: Table 'umm.db_user' doesn't exist
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970) at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1020)
at sun.reflect.GeneratedMethodAccessor187.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498)
org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426) org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor$StatementProxy.invoke(StatementDecoratorInterceptor.java:261)
at com.sun.proxy.$Proxy102.executeQuery(Unknown Source) org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
    ... 134 common frames omitted
Grails application running at http://localhost:9091/umm in environment: development

【问题讨论】:

  • 如果没有看到配置的其余部分,很难确定,但看起来dbCreate 可能被设置为阻止 DDL 在启动时生成并发送到数据库的东西。在您的应用程序中是否有另一个dataSource 配置为dbCreate 提供了与上面配置sn-p 中显示的值不同的值?如果不是,请在github.com/grails/gorm-hibernate5/issues 提出问题并链接到我们可以调查的示例应用程序。
  • dbCreate 在整个应用程序中重复两次,并且在两个地方都设置为 update。请让我知道您还想查看哪些其他配置?
  • 我无法重现您所看到的行为。如果您提出问题并分享您的完整配置,我们将对其进行故障排除并理顺它。感谢您的反馈。

标签: mysql hibernate grails groovy grails-orm


【解决方案1】:

做三件事,

  1. 改变方言

    org.hibernate.dialect.MySQL8Dialect

  2. 和/更新mysql驱动依赖

    mysql:mysql-connector-java:8.0.17

  3. 确保您在配置中使用了正确的类型 (Holders.config.ProfilePictureType)(可能是长文本)

【讨论】:

  • 通过将 sqlType 从 longblob 更改为 longtext 解决了我的问题。感谢您的帮助。
  • @Sikander 你是说8.0.26-cluster 不兼容吗?
  • 我在 8.0.26-cluster 上测试了这个。使用此版本也可以正常工作,我的问题通过使用上述要点中的第 3 点得到解决。由于longblob sqlType,没有自动生成表
猜你喜欢
  • 2018-04-25
  • 2020-11-25
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2012-09-11
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多