域 class 的 Grails 包更改导致 DuplicateMappingException

Grails package change for domain class caused DuplicateMappingException

在通过教程开始学习 Grails 时,我犯了一个错误 运行:

grails create-domain-class com.FooBar

而不是:

grails create-domain-class com.acme.FooBar

很明显我犯了一个错误,所以我尝试了以下操作:

  1. 搜索了一个反转create-domain-class命令的函数,好像没有。
  2. 在网络上搜索了建议,一致认为您可以删除域 class 文件、任何关联的视图和测试,然后为了安全起见 运行 对您的 class 在你的项目目录中找到你可能遗漏的任何参考的名称。这些我都做到了。
  3. 然后我 运行 创建 com.acme.FooBar 的正确命令,它起作用了。

在此之后应用无法 运行 并报告以下错误:

org.hibernate.DuplicateMappingException: duplicate import: FooBar refers to both com.acme.FooBar and com.FooBar (try using auto-import="false")

在com.acme.FooBar中添加以下代码后:

...
static mapping = {
    autoImport false
}
...

该应用现在 运行 符合预期。

然而,作为一名偶尔重构包的经验丰富的 Java 开发人员,我想了解如何在不导致 DuplicateMappingException 或求助于 "autoImport false" 解决方案的情况下进行重构。

谢谢。

你不应该这样做

static mapping = {
        autoImport false
    }

因为,通过这样做,您说过不要只按名称检查域,还要查找包。因此,一旦你这样做,你将不得不在你的查询/hqls 中使用 class 的完全限定名称,这有时可能会很痒。

您应该完全删除域,即

  1. remove the Domain
  2. remove the view folder creating by default with very same name and so do the controller
  3. Now, do grails clean-all(Make it a thumb rule to use grails clean-all first for any issue unexpectedly occuring).
  4. To be more accurate do remove target directory from your project and then do run grails run-app.

同样的事情我已经做了很多次,并通过上述步骤解决了。

希望对您有所帮助。