简体   繁体   English

域类的Grails软件包更改导致DuplicateMappingException

[英]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: 显而易见,我犯了一个错误,所以尝试了以下操作:

  1. Searched for a function that reverses the create-domain-class command, it seems there isn't one. 搜索了一个与create-domain-class命令相反的函数,似乎没有一个。
  2. Searched for advice on the web and the consensus is that you can delete a domain class file, any associated views and tests, then to be safe run a text search for your class name in your project directory for any references you may have missed. 在网上搜索建议,并达成共识,即您可以删除域类文件,所有关联的视图和测试,然后为了安全起见,在项目目录中运行文本搜索类名称,以查找您可能错过的所有引用。 I have done all this. 我都做完了
  3. Then I ran the correct command to create com.acme.FooBar, which worked. 然后,我运行正确的命令来创建com.acme.FooBar,该命令可以正常工作。

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 您应该完全删除域,即

  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). 现在,请执行grails clean-all(为任何意外发生的问题首先使用grails clean-all制定经验法则)。
  4. 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.

相关问题 Grails-严重的IllegalStateException-类[]上的方法在Grails应用程序之外使用 - Grails - severe IllegalStateException - Method on class [] was used outside of a Grails application ClassNotFoundException用于同一个包中的类 - ClassNotFoundException for class in same package 原因:没有为名为“成功”的结果指定结果类型,也许父包未指定结果类型? - Caused by: No result type specified for result named 'success', perhaps the parent package does not specify the result type? 在Hibernate中的模型(域)类中使用枚举-无法解析属性-异常 - Using Enum in Model (domain) Class with hibernate - could not resolve property - exception 原因:java.lang.NoClassDefFoundError:当tomcat / lib下存在类时? - Caused by: java.lang.NoClassDefFoundError : when class exists under tomcat/lib? 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type Java:创建自定义异常类包 - Java: create custom exception class package.Exception 将类添加到Swing包中-结果是可见函数上的IllegalAccessError - Adding a class to the Swing package — result is an IllegalAccessError on a visible function Grails IncompatibleClassChangeError - Grails IncompatibleClassChangeError NoClassDefFoundError:…由ClassNotFoundException引起 - NoClassDefFoundError: … Caused by ClassNotFoundException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM