我有一个 Spring 4.1.0 后端应用程序,其中使用 Maven 作为构建工具,在 JPA 中注释了域类(使用 Hibernate 4.3.5 作为持久性提供程序)。我现在想向这个应用程序添加一个 Web 前端组件,并决定使用 Grails 3.x 作为我的 Web 框架。我想通过 Grails 重新使用我现有的 JPA 注释域类,然后使用 generate-all 为每个域模型创建 Controller 和 View 。我的第一个里程碑目标是从这个 Web 应用程序中获得旧域模型的基本 CRUD 功能。
按照我找到的信息in the Grails documentation和 in some older blog posts以及一些稍微相关的 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"
...
}
到目前为止,IntelliJ IDEA 14 没有警告或错误。然后我创建了 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>
这里也没有来自 IDE 的提示。
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;
...
}
然后我跳入 Grails 控制台并尝试为 City 域类生成 Controller 和 View :
grails generate-all com.my-company.my-spring-app.domain.City
但我只是得到一个域类未找到错误:
| Error domain class not found for name com.my-company.my-spring-app.domain.City
我很快创建了一个 Grails 2.5.0 应用程序,将 my-spring-app.jar 放在 lib/中,然后再次尝试以查看它是否是 Grails 3.0.1 的“出血边缘”问题,但得到了相同的结果。
有谁知道这里发生了什么?如何在 Grails 3.x 中重用旧的 JPA 域类,以便仅使用一组域实体来保持 DRY?
最佳答案
我通过放置 hibernate.cfg.xml
解决了类似的问题在 grails-app/conf
(不在 hibernate 子目录中)如 mapping-with-hibernate-annotations-in-grails-3-0-1 中所述.
关于spring - Grails 3.x : Re-using JPA/Hibernate Domain classes: Domain class not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908232/