Hibernate 无法加载实体错误
我刚刚开始在一个新项目中使用 Hibernate,对于我的第一个实体,我遇到了一个错误,我似乎无法弄清楚。
我现在的架构只有两个表:大洲和国家,其中国家/地区有一个 ContinentalID 外键。
当我尝试运行调用大陆实体的代码时,我得到一个空白页面。所有处理都会停止,但不会显示任何错误。当我通过 MXUnit 运行代码时,我实际上收到了一个错误。错误消息只是无法加载实体:[大陆#1]
。异常的原因是org.hibernate.exception.SQLGrammarException
。这就是我得到的全部。
我的实际实体代码是:
Continent.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;
}
}
Country.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'];}
}
中,我找不到任何关于我能理解的错误消息的信息,因此这可能只是一个无效的语法。
I just started using Hibernate on a new project, and with my first entity, I got an error, that I can't seem to figure out.
My schema right now is just two tables, continents and countries, where country has a continentid foreign key.
When I try to run code that calls the continents entity, I get a blank page. All processing just stops, but no error is displayed. When I run the code through MXUnit, I actually get an error. The error message is simply Could Not Load an Entity: [continent#1]
. The cause of the exception is org.hibernate.exception.SQLGrammarException
. That is all the I get.
My actual entity code is:
Continent.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;
}
}
Country.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;
}
}
And the code that calls the method. It is in a 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'];}
}
I couldn't find any information really about the error message that I could understand, so this could simply be an invalid syntax thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是我使用了错误的属性来指定要映射到对象中的表名称。该属性应该是 table='' 所以我的休眠对象应该如下所示:
Continent.cfc
Country.cfc
The issue was that I was using the wrong attribute to specify the table name to be mapped in the objects. The attribute should be table='' so my hibernate objects should look like this:
Continent.cfc
Country.cfc
可能是表结构与java类中映射的表结构不同。
It may be the case that table structure is different from what is been mapped in java class.
您的数据库架构是什么样的?我见过当对象中的数据类型与数据模型中的数据类型不一致时抛出 SQL Grammer 异常。
What does your database schema look like? I've seen SQL Grammer execptions thrown when the datatypes in your objects don't align with the datatypes in your datamodel.
你的数据库引擎是什么?
我们可以看看你的 orm 应用程序配置吗?
就我个人而言,我从未见过属性
'datatype'
并且在文档中也没有找到它。如果您想强制使用数据类型,我建议您使用
'sqlType'
。通常我将它与 datetime 一起使用:我必须指定
sqltype="datetime"
因为type="date"
还不够。What's your DB engine ?
Can we see your orm application config ?
Personally I have never seen the property
'datatype'
and I didn't find it in the documentation.I recommend you to use
'sqlType'
if you want to force the data type. Typically I use it with datetime :I have to specify
sqltype="datetime"
becausetype="date"
is not enough.确保显式提供配置
ID
和数据类型。例如,将其更改
为:
Make sure that the configuration
ID
is provided with the data type explicitly.For example, change this:
To this: