# 15. Grails and Hibernate

If GORM (Grails Object Relational Mapping) is not flexible enough for your liking you can alternatively write some or all of your domain classes in Java or re-use an existing domain model that has been mapped using Hibernate. To do this create a `hibernate.cfg.xml` file in the `grails-app/conf/hibernate` directory of your project and add the corresponding HBM mapping xml files for your domain classes. You can do this manually or by running the create-hibernate-cfg-xml script.

> For more info on how to do this read the documentation on mapping at the Hibernate Website

This will allow you to map Grails domain classes onto a wider range of legacy systems and have more flexibility in the creation of your database schema.

Additionally, you will still be able to call all of the dynamic persistent and query methods allowed in GORM!

## 15.1 Mapping with Hibernate Annotations

Grails also supports creating domain classes mapped with Hibernate's Java 5.0 Annotations support.

To create an annotated domain class, create a new class in `src/java` and use the annotations defined as part of the EJB 3.0 spec (for more info on this see the Hibernate Annotations Docs):

    package com.books;
    @Entity
    public class Book {
        private Long id;
        private String title;
        private String description;
        private Date date;    @Id
        @GeneratedValue
        public Long getId() {
            return id;
        }    public void setId(Long id) {
            this.id = id;
        }    public String getTitle() {
            return title;
        }    public void setTitle(String title) {
            this.title = title;
        }    public String getDescription() {
            return description;
        }    public void setDescription(String description) {
            this.description = description;
        }
    }

Once that is done you need to register the class with the Hibernate `sessionFactory`, to do you need to add entries to the `grails-app/conf/hibernate/hibernate.cfg.xml` file as follows:

    <!DOCTYPE hibernate-configuration SYSTEM
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <mapping package="com.books" />
            <mapping class="com.books.Book" />
        </session-factory>
    </hibernate-configuration>

By default the `hibernate.cfg.xml` file is located in the `grails-app/conf/hibernate` directory. If you wish to change this you can do so by specifying an alternative location in `grails-app/conf/DataSource.groovy`:

    hibernate {
    	config.location = "file:/path/to/my/hibernate.cfg.xml"
    }

Or even a list of locations:

    hibernate {
    	config.location = ["file:/path/to/one/hibernate.cfg.xml",
                          "file:/path/to/two/hibernate.cfg.xml"]
    }

When Grails loads it will register the necessary dynamic methods with the class. To see what else you can do with a Hibernate domain class see the section on Scaffolding.

## 15.2 Further Reading

Grails committer, Jason Rudolph, took the time to write many useful articles about using Grails with custom Hibernate mappings including:

- Hoisting Grails to Your Legacy DB - An excellent article about using Grails with Hibernate XML
- Grails + EJB3 Domain Models - Another great article about using Grails with EJB3-style annotated domain models