gpt4 book ai didi

grails - 将外部域类导入Grails

转载 作者:行者123 更新时间:2023-12-02 13:51:44 25 4
gpt4 key购买 nike

我正在尝试创建一个示例项目,其中域类在外部常规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应用中,

    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 (我尝试了一种脚手架和编码 Controller ,但均失败了):
    ...
    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项目中使用这些域,这就是为什么我首先选择这种方式的原因)。
  • 最佳答案

    最后解决它,手动创建一个“二进制”插件。

    让我们逐步看一下:

    书域

    树结构

    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类转换为真实的Domains。

    希望能帮助到你。

    关于grails - 将外部域类导入Grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23654116/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com