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.
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 :NOTE: Entity files should be a groovy file mapped with
@Entity
and inheriting interfaceGormEntity
annotation for supporting Groovy's dynamic finders.Example :
Hibernate mapping :
Here is the full working example : grails-hibernate-xml-config-example