将外部域类导入Grails

时间:2014-05-14 11:59:06

标签: grails gorm

我正在尝试创建一个示例项目,其中域类位于外部普通groovy项目中,然后在grails应用程序中使用(请参阅https://github.com/ivanarrizabalaga/grails-domain-griffon):

  • book-svr
  • 书共同

我也在关注grails指南以获取此内容(请参阅http://grails.org/doc/latest/guide/hibernate.html),但导入的类未被识别为域类。

相关部分:

外部域类

package com.nortia.book
import grails.persistence.Entity

@Entity
class Book implements Serializable{

    private static final long serialVersionUID = 1L;

    String title
    String author

    static constraints = {
        title blank:false
        author blank:false
    }
}

的build.gradle

....
dependencies {
    // We use the latest groovy 2.x version for building this library
    compile 'org.codehaus.groovy:groovy:2.1.7'

    compile "org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE"
    compile "org.grails:grails-spring:2.3.7"
....

在grails app中,

hibernate.cfg.xml中

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
    'http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>
    <session-factory>
        <mapping package='com.nortia.book' />       
        <mapping class='com.nortia.book.Book' />
    </session-factory>
</hibernate-configuration>

BookController.groovy (我试过一个脚手架和编码控制器,但都失败了):

...
class BookController{
    static scaffold=Book
}
...

console.log (错误):

ERROR ScaffoldingGrailsPlugin  - Cannot generate controller logic for scaffolded class class com.nortia.book.Book. It is not a domain class!

最后,初始化时出现可疑的日志消息

DEBUG cfg.Configuration  - session-factory config [null] named package [com.nortia.book] for mapping
INFO  cfg.Configuration  - Mapping package com.nortia.book
WARN  cfg.AnnotationBinder  - Package not found or wo package-info.java: com.nortia.book
DEBUG cfg.Configuration  - session-factory config [null] named class [com.nortia.book.Book] for mapping
INFO  cfg.Configuration  - Configured SessionFactory: null

所以我想知道:

  • 缺少的是什么?
  • 根据文档,看起来外部'域'必须是java类,但这不是我的目的的好选择。
  • 我还没有尝试使用grails二进制插件而不是一个常规项目,它是要走的路吗? (我需要在griffon项目中使用域名,这就是我先选择这种方式的原因。)

1 个答案:

答案 0 :(得分:4)

最后解决它创造一种&#39;&#39;手动创建二进制插件。

让我们一步一步看看:

书域

树状结构

src
  main
    groovy
      demo
        Book.groovy
    resources
      META-INF
        grails-plugin.xml
build.gradle

<强> Book.groovy

package demo

import grails.persistence.Entity

@Entity
class Book{

    String title

    static constraints = {
        title blank: false
    }
}

<强>的grails-plugin.xml的

<plugin name='book-domain' version='1.0' grailsVersion='2.3 &gt; *'>
  <author>Ivan Arrizabalaga</author>
  <title>External domains</title>
  <description>An external domain plugin</description>
  <documentation>http://grails.org/plugin/book-domain</documentation>
  <type>demo.BookDomainGrailsPlugin</type>
  <packaging>binary</packaging>
  <resources>
    <resource>demo.Book</resource>
  </resources>
</plugin>

<强>的build.gradle

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'arrizabalaga' at '5/26/14 12:34 PM' with Gradle 1.11
 *
 * This generated file contains a sample Groovy project to get you started.
 * For more details take a look at the Groovy Quickstart chapter in the Gradle
 * user guide available at http://gradle.org/docs/1.11/userguide/tutorial_groovy_projects.html
 */

// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
apply plugin: 'maven'

group = 'demo'
version = '1.0'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'maven central' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    mavenLocal()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // We use the latest groovy 2.x version for building this library
    compile 'org.codehaus.groovy:groovy-all:2.1.9'
    //compile "org.grails:grails-datastore-gorm-hibernate4:3.1.0.RELEASE"
    compile "org.grails:grails-datastore-gorm-hibernate:3.1.0.RELEASE"
    compile "commons-lang:commons-lang:2.6"

    // We use the awesome Spock testing and specification framework
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
    testCompile 'junit:junit:4.11'
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

使用它

现在可以构建项目( gradle install 将其发布到您的本地maven仓库中)并在任何给定项目中使用(声明正确的依赖项)。

如果使用jar的项目是Grails应用程序,它会自动将@Entity类转换为真实域。

希望它有所帮助。