[英]Grails package change for domain class caused DuplicateMappingException
While working through a tutorial to start learning Grails, I made a mistake and ran: 在学习教程以开始学习Grails时,我犯了一个错误并运行:
grails create-domain-class com.FooBar
instead of: 代替:
grails create-domain-class com.acme.FooBar
It was immediately obvious I had made an error so I tried the following: 显而易见,我犯了一个错误,所以尝试了以下操作:
After this the app fails to run and reports the following error: 此后,该应用程序无法运行,并报告以下错误:
org.hibernate.DuplicateMappingException: duplicate import: FooBar refers to both com.acme.FooBar and com.FooBar (try using auto-import="false")
After adding the following code to com.acme.FooBar: 将以下代码添加到com.acme.FooBar之后:
...
static mapping = {
autoImport false
}
...
The app now runs as expected. 该应用程序现在可以按预期运行。
However as an experienced Java developer who occasionally does refactor a package I would like to understand how to do that without causing a DuplicateMappingException or resorting to the "autoImport false" solution. 但是,作为经验丰富的Java开发人员,偶尔会重构软件包,我想了解如何做到这一点而又不会导致DuplicateMappingException或求助于“ autoImport false”解决方案。
Thanks. 谢谢。
You shouldn't be doing 你不应该那样做
static mapping = {
autoImport false
}
As, by doing this you said that don't check for domain just by name and look up for package as well. 因此,您说过不要仅按名称检查域,也不要查找软件包。 Hence, once you do that you will have to use Fully qualified name of the class in your queries / hqls which may itch sometimes.
因此,一旦这样做,您将不得不在查询/ hqls中使用类的完全限定名称,这有时会发痒。
You should be removing the Domain completely ie 您应该完全删除域,即
- remove the Domain
删除域
- remove the view folder creating by default with very same name and so do the controller
删除默认情况下使用相同名称创建的视图文件夹,控制器也是如此
- Now, do grails clean-all(Make it a thumb rule to use grails clean-all first for any issue unexpectedly occuring).
现在,请执行grails clean-all(为任何意外发生的问题首先使用grails clean-all制定经验法则)。
- To be more accurate do remove target directory from your project and then do run grails run-app.
为了更准确,请从项目中删除目标目录,然后再运行grails run-app。
I had done very same thing many times and got it resolved by above steps. 我做了很多相同的事情,并通过上述步骤解决了它。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.