grailsgrails-domain-classgrails-constraints

Grails Constraints with Java Classes and Hibernate Mappings


I have the following Java class defined in src/java

package org.davisworld.trip;

public class AirportHbm {
  private long id;
  private String name;
  private String iata;
  private String state;
  private String lat;
  private String lng;

  // getters/setters defined
}

I have the hbm.cfg.xml file defined as follows in conf/hibernate:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping resource="AirportHbm.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

I have the AirportHbm.hbm.xml file configured as follows in conf/hibernate:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.davisworld.trip.AirportHbm" table="usgs_airports">
        <id name="id" column="airport_id">
            <generator class="native"/>
         </id>          
        <property name="name" type="java.lang.String">
            <column name="airport_name" not-null="true" />
        </property>
        <property name="iata" type="java.lang.String">
            <column name="locid" not-null="true" />
        </property>
        <property name="state" />
        <property name="lat" column="latitude" />
        <property name="lng" column="longitude" />
    </class>
</hibernate-mapping>

And lastly, I have an AirportHbmConstraints.groovy file in the src/java folder:

package org.davisworld.trip

class AirportHbmConstraints {
    static constraints = {
        name()
        iata(maxSize:3)
        state(maxSize:2)
        lat()
        lng()
    }
}

When I try to run the app, I get this error when Spring is initializing the web app context:

Caused by: java.lang.ClassCastException: org.davisworld.trip.AirportHbmConstraints cannot be cast to groovy.lang.Script

The tutorial I was following originally said that the AirportHbmConstraints.groovy file shouldn't have a class; it should just be a script:

package org.davisworld.trip


static constraints = {
        name()
        iata(maxSize:3)
        state(maxSize:2)
        lat()
        lng()
}

But when I do that, STS gives me a compiler error:

Groovy:Modifier 'static' not allowed here.

Anyone know what I'm doing wrong? What is the correct way to apply constraints in Groovy to a Java domain class?

Many thanks, Vito


Solution

  • When using constraints scripts with a Hibernate domain you do not use a class declaration or the static modifier as explained in section 15.3 Adding Constraints of the Grails documentation.

    The correct constraint script would be:

    constraints = {
        iata maxSize: 3
        state maxSize: 2
    }
    

    Note that fields without a constraint and the parentheses on the field declaration are optional.