1

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.

2
  • Does it work if you use the grails-app/conf/hibernate/ directory to hold the config file? Commented Oct 14, 2020 at 18:39
  • @JeffScottBrown no its not working that way either. Commented Oct 15, 2020 at 8:37

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.