0

我刚开始在一个新项目中使用 Hibernate,在我的第一个实体中,我遇到了一个错误,我似乎无法弄清楚。

我现在的架构只有两个表,大陆和国家,其中国家有一个大陆外键。

当我尝试运行调用大陆实体的代码时,我得到一个空白页。所有处理都会停止,但不会显示错误。当我通过 MXUnit 运行代码时,我实际上得到了一个错误。错误信息很简单Could Not Load an Entity: [continent#1]。异常的原因是org.hibernate.exception.SQLGrammarException。这就是我得到的全部。

我的实际实体代码是:

大陆.cfc

component tablename='continents' persistent=true output=false{
    property name='id' type='numeric' fieldtype='id' datatype='integer';
    property name="name" type='string' length='45';
    property name='bonus' type='numeric';
    property name='countries' type='array' fieldtype='one-to-many' cfc='Country' singularname='country' inverse='true' fkcolumn='continentid' cascade='all-delete-orphan'; 

    public Country function init(string name='', numeric bonus=0){
        setName(Arguments.name);
        return this;
    }
}

国家.cfc

component tablename='countries' persistent=true output=false{
    property name='id' type='numeric' fieldtype='id' datatype='integer' generator="identity";
    property name="name" type='string' length='45';
    property name='continent' fieldtype='many-to-one' fkcolumn='continentid' cfc='Continent' missingRowIgnored=true;

    public Country function init(string name=''){
        setName(Arguments.name);
        return this;
    }
}

以及调用该方法的代码。它在一个 ColdSpring bean 中

大陆豆.cfc

component {
    property name="continent" type="any";

    public any function init(any continent=''){
        if(len(Arguments.continent))setContinent(Arguments.continent);
        return this;
    }

    public any function getContinent(){
        return continent;
    }
    public void function setContinent(numeric continent){
        continent = EntityLoad('Continent', Arguments.continent, true);
    }

    public void function setMapService(mapService){variables.instance['mapService'] = Arguments.mapService;}
    public any function getMapService(){return variables.instance['mapService'];}
}

我找不到任何关于我能理解的错误消息的真正信息,所以这可能只是一个无效的语法。

4

5 回答 5

4

问题是我使用了错误的属性来指定要在对象中映射的表名。该属性应该是 table='' 所以我的休眠对象应该是这样的:

大陆.cfc

component table='continents' persistent=true output=false{
}

国家.cfc

component table='countries' persistent=true output=false{
}
于 2011-06-01T13:23:41.813 回答
1

可能是表结构与 java 类中映射的不同。

于 2012-11-21T12:11:09.520 回答
0

您的数据库架构是什么样的?当您的对象中的数据类型与您的数据模型中的数据类型不一致时,我已经看到抛出 SQL Grammer execptions。

于 2011-06-01T00:32:16.167 回答
0

你的数据库引擎是什么?我们可以看到您的 orm 应用程序配置吗?

就我个人而言,我从未见过该物业'datatype',也没有在文档中找到它。'sqlType'如果要强制数据类型,我建议您使用。通常我将它与 datetime 一起使用:

property name="creationDateTime" type="date" sqltype="datetime";

我必须指定sqltype="datetime",因为type="date"还不够。

于 2011-06-01T08:20:12.323 回答
0

确保为配置ID提供了明确的数据类型。

例如,改变这个:

<id name="id" column="ID"/>

对此:

<id name="id" column="ID" type="java.lang.Long"/>
于 2016-08-12T10:52:31.127 回答