【问题标题】:Hibernate Could Not Load an Entity ErrorHibernate 无法加载实体错误
【发布时间】:2019-01-16 12:00:47
【问题描述】:

我刚开始在一个新项目中使用 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 中

ContinentBean.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'];}
}

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

【问题讨论】:

  • 唯一让我觉得奇怪的是“数据类型”。我从未使用过它,我认为只有'ormtype''sqltype'。您可以尝试删除它并执行ORMReload() 并查看它是否有效?另外,仔细查看堆栈跟踪,有时您会在那里看到一些有用的东西。并打开 ormsettings.logsql 来检查生成的 SQL 是否有意义。

标签: hibernate coldfusion railo


【解决方案1】:

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

大陆.cfc

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

国家.cfc

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

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

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

        【讨论】:

          【解决方案5】:

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

          例如,改变这个:

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

          到这里:

          <id name="id" column="ID" type="java.lang.Long"/>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-22
            • 1970-01-01
            • 2020-07-22
            • 1970-01-01
            • 2023-01-19
            • 1970-01-01
            相关资源
            最近更新 更多