简体   繁体   English

Grails 3.x:重用JPA / Hibernate Domain类:找不到域类

[英]Grails 3.x: Re-using JPA/Hibernate Domain classes: Domain class not found

I have a Spring 4.1.0 back-end application with domain classes annotated in JPA (w/Hibernate 4.3.5 as the persistence provider) using Maven as the build tool. 我有一个Spring 4.1.0后端应用程序,其中使用Maven作为构建工具,在JPA(使用Hibernate 4.3.5作为持久性提供程序)中注释了域类。 I now want to add a web front-end component to this app and have decided to use Grails 3.x as my web framework. 我现在想在这个应用程序中添加一个Web前端组件,并决定使用Grails 3.x作为我的Web框架。 I want to re-use my existing JPA annotated domain classes with Grails and then use generate-all to create controllers and views for each domain model. 我想重新使用我现有的带有Grails的JPA带注释的域类,然后使用generate-all为每个域模型创建控制器和视图。 My first milestone goal is to get basic CRUD functionality on the old domain models from this web app. 我的第一个里程碑目标是通过此Web应用程序在旧域模型上获得基本的CRUD功能。

Following the information I found in the Grails documentation and in some older blog posts as well as some slightly related SO posts, I created a new Grails project and packaged up my existing project as a jar and installed it (I ran mvn install -DskipTests to be exact) and then added it to build.gradle (actually I just want to have one project in the end, but I thought I'd try it this way first because I don't want to wrestle with having Maven and Gradle in the same project yet): 根据我在Grails文档一些较旧的博客文章以及一些稍微相关的SO帖子中找到的信息,我创建了一个新的Grails项目并将我现有的项目打包为jar并安装它(我运行了mvn install -DskipTests到确切地说,然后将它添加到build.gradle(实际上我只想最终有一个项目,但我想我会先尝试这种方式,因为我不想和Maven和Gradle一起努力同一个项目):

 repositories {
 ...
 mavenLocal()
 ...
}
 dependencies {
 ...

 compile "com.my-company:my-spring-app:1.0.0.CI-SNAPSHOT"
 ...
}

No warnings or errors from IntelliJ IDEA 14 so far. 到目前为止,IntelliJ IDEA 14没有任何警告或错误。 Then I created the grails-app/conf/hibernate/hibernate.cfg.xml file and tried putting just one of my old JPA annotated entities in it for now: 然后我创建了grails-app / conf / hibernate / hibernate.cfg.xml文件,并尝试将其中一个旧的JPA注释实体放入其中:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping package="com.my-company.my-spring-app.domain" />
        <mapping class="com.my-company.my-spring-app.domain.City" />
    </session-factory>
</hibernate-configuration>

No complaints from the IDE here either. 此处也没有来自IDE的投诉。 The City.java entity class looks something like: City.java实体类看起来像:

package com.my-company.my-spring-app.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * City generated by hbm2java
 */
@Entity
@Table(name = "city",
     schema = "public",
     uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class City implements java.io.Serializable {

private static final long serialVersionUID = 4674557242772722625L;

@Id
@SequenceGenerator(name = "city_gen",
                   schema = "public",
                   sequenceName = "city_id_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator = "city_gen")
@Column(name = "id",
        unique = true,
        nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countryid",
            nullable = false)
// @JoinColumn(name = "country_id", nullable = false)
private Country country;

@Column(name = "name",
        unique = true,
        length = 200)
private String name;
...
}

Then I jumped into the Grails console and tried to generate the controller and views for the City domain class: 然后我跳进Grails控制台并尝试为City域类生成控制器和视图:

grails generate-all com.my-company.my-spring-app.domain.City

But I just get a Domain class not found error: 但我只是得到一个Domain class not found错误:

| Error domain class not found for name com.my-company.my-spring-app.domain.City

I quickly created a Grails 2.5.0 app, put the my-spring-app.jar in lib/ and tried this again to see if it was an issue with the 'bleeding edge' Grails 3.0.1 but got the same result. 我快速创建了一个Grails 2.5.0应用程序,将my-spring-app.jar放入lib /并再试一次,看看它是否与'前沿'Grails 3.0.1有关,但结果相同。

Does anyone know what's going on here? 有谁知道这里发生了什么? How can I re-use my old JPA domain classes with Grails 3.x so that I can stay DRY with only one set of domain entities? 如何使用Grails 3.x重新使用旧的JPA域类,以便只使用一组域实体来保持DRY?

我通过将hibernate.cfg.xml放在grails-app/conf (不在hibernate子目录中)解决了类似的问题,如mapping-with-hibernate-annotations-in-grails-3-0-1中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM