Grails 与 Java 类和 Hibernate 映射的约束

发布于 2025-01-05 15:46:41 字数 2606 浏览 0 评论 0原文

我在 src/java 中定义了以下 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
}

我在 conf/hibernate 中定义了 hbm.cfg.xml 文件,如下所示:

<?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>

我在 conf/hibernate 中配置了 AirportHbm.hbm.xml 文件,如下所示:

<?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>

最后,我有src/java 文件夹中的 AirportHbmConstraints.groovy 文件:

package org.davisworld.trip

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

当我尝试运行应用程序时,当 Spring 初始化 Web 应用程序上下文时出现此错误:

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

我最初遵循的教程说 AirportHbmConstraints.groovy 文件不应该有类;它应该只是一个脚本:

package org.davisworld.trip


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

但是当我这样做时,STS 给我一个编译器错误:

Groovy:Modifier 'static' not allowed here。

有人知道我做错了什么吗?将 Groovy 中的约束应用于 Java 域类的正确方法是什么?

非常感谢, 维托

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2025-01-12 15:46:41

在 Hibernate 域中使用约束脚本时,您不使用 class 声明或 static 修饰符,如 15.3 添加约束

正确的约束脚本是:

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

请注意,没有约束的字段和字段声明上的括号是可选的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文