How to integrate hibernate xml mapping of java classes in grails 4?

412 views Asked by At

I am trying to map a POJO by hibernate xml mapping configuration in my grails app. This is working fine in grails 2.x version but in grails 4 it is not taking hibernate config which is at location :

grails-app/conf/hibernate.cfg.xml

which is this :

<?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 resource='com.prabin.test.hbm.xml'/>
    </session-factory>

</hibernate-configuration>

com.prabin.test.hbm.xml is also at same location as hibernate.cfg.xml

com.prabin.test.hbm.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.prabin.Prabin" table="prabin">
        <id name="id" column="prabin_id">
            <generator class="identity"/>
        </id>
    </class>
</hibernate-mapping>

My Pojo is at location :

src/main/java/com/prabin/Prabin.java

which is :

package com.prabin;

public class Prabin {
    Integer  id;

    // Getters and Setters
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
}

App is not taking hibernate config file and hence not creating any table for my pojo. Hibernate config file is totally ignored.

1

There are 1 answers

0
Prabin Upreti On

Grails support team helped me through this, the process is as follows:

The hibernate mapping file : hibernate.mapping.xml should be in /src/main/resources directory and mapped in application.yml as follows :

hibernate:
    mappingLocations: classpath:hibernate.mapping.xml

NOTE: Entity files should be a groovy file mapped with @Entity and inheriting interface GormEntity annotation for supporting Groovy's dynamic finders.

Example :

@Entity
class Employee implements GormEntity<Employee> {
    Integer id
    String firstName
    String lastName
    Double salary
}

Hibernate mapping :

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name = "com.objectcomputing.example.Employee" table = "EMPLOYEE">

        <meta attribute = "class-description">
            This class contains the employee detail.
        </meta>

        <id name = "id" type = "int" column = "id">
            <generator class="native"/>
        </id>

        <property name = "firstName" column = "first_name" type = "string"/>
        <property name = "lastName" column = "last_name" type = "string"/>
        <property name = "salary" column = "salary" type = "double"/>

    </class>
</hibernate-mapping>

Here is the full working example : grails-hibernate-xml-config-example