Grails 3.x: JPA / Hibernate domain class reuse: domain class not found

I have an external Spring 4.1.0 application with domains annotated in JPA (w / Hibernate 4.3.5 as persistence provider) using Maven as build tool. Now I want to add a web interface for this application and decided to use Grails 3.x as my web framework. I want to reuse existing existing annotated domains JPA classes with Grails and then use generate-all to generate controllers and views for each domain model. My first goal is to get basic CRUD functionality on older domain models from this web app.

Following the information, I found in the Grails documentation and in some old blog posts , as well as some slightly related SO posts, I created a new Grails project and packaged my existing project as a jar and installed it (I did mvn install -DskipTests if be exact) and then added it to build.gradle (actually I just want to have one project at the end, but I thought I'd try it first because I don't want to struggle with Maven and Gradle in the same project yet):

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

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

      

So far there are no warnings or errors from IntelliJ IDEA 14. Then I created a grails-app / conf / hibernate / hibernate.cfg.xml file and tried to put just one of my old JPA annotated objects in it:

<?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. The City.java entity class looks something like this:

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 create a controller and views for the City city class:

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

      

But I just get the domain class error:

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

      

I quickly created a Grails 2.5.0 application, put my-spring-app.jar in lib / and tried it again to see if there was a Grails 3.0.1 bleeding edge issue, but got the same result.

Does anyone know what's going on here? How can I reuse my old JPA domain classes with Grails 3.x so that I can stay DRY with only one set of domain objects?

+3


source to share


1 answer


I resolved a similar issue by putting hibernate.cfg.xml

in grails-app/conf

(not inside the hibernate subdirectory) as described in mapping-with-hibernate-annotations-in-grails-3-0-1 .



0


source







All Articles