为什么 ColdFusion/Hibernate 将实体名称作为前缀添加到生成的 SQL 中的列名称中?
我正在运行 ColdFusion 9.01,并在撰写本文时应用了所有最新的修补程序。我已经在生产站点上使用 ColdFusion 9 的 Hibernate 集成一年多了,现在需要关联两个现有实体;然而,Hibernate 为多对多关系生成了不正确的列名。
我现有的两个实体是演示者和产品。我将 dbcreate 设置为“none”,并在 Oracle 11g 数据库上使用 autogenmap 的默认值。因此,Hibernate 不负责对我的数据库进行架构更改。
这是我添加到 Presenter 实体的新属性。
/** Relation to any episodes associated to this presenter. **/
property name='Products' fieldtype='many-to-many' lazy='extra'
singularName="Product" cfc='platform.shared.models.entities.Product' fkcolumn='PRESENTER_ID'
linktable='PRESENTERS_TO_PRODUCTS' cacheuse = 'read-only' cachename = 'EntityCache' inversejoincolumn='PRODUCT_ID';
这是 Hibernate 生成的无效 SQL。
select count(Product_product_id) from PRESENTERS_TO_PRODUCTS where Presenter_presenter_id =?
我不确定为什么 Hibernate 在列前加上实体的名称前缀。 SQL 应如下所示:
select count(product_id) from PRESENTERS_TO_PRODUCTS where presenter_id =?
当我诊断此问题时,我保存了 Hibernate 生成的 HBXML 文件。我在下面包含了演示者实体的完整文件。
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="Presenter" lazy="true"
name="cfc:platform.shared.models.entities.Presenter" table="PRESENTERS">
<id name="presenter_id" type="integer">
<column name="presenter_id"/>
<generator class="increment"/>
</id>
<property name="presenter_bio" type="text">
<column name="presenter_bio"/>
</property>
<property name="presenter_name" type="text">
<column name="presenter_name"/>
</property>
<property name="presenter_website" type="text">
<column name="presenter_website"/>
</property>
<property name="presenter_company" type="text">
<column name="presenter_company"/>
</property>
<property name="presenter_internalname" type="text">
<column name="presenter_internalname"/>
</property>
<property name="presenter_logo" type="integer">
<column name="presenter_logo"/>
</property>
<property name="presenter_description" type="text">
<column name="presenter_description"/>
</property>
<property name="deleted" type="integer">
<column name="deleted"/>
</property>
<property name="presenter_is_evangelist" type="integer">
<column name="presenter_is_evangelist"/>
</property>
<property name="presenter_is_expert" type="integer">
<column name="presenter_is_expert"/>
</property>
<property name="presenter_about" type="text">
<column name="presenter_about"/>
</property>
<bag lazy="extra" name="Episodes" table="ASSET_TO_PRESENTERS">
<cache region="EntityCache" usage="read-only"/>
<key column="PRESENTER_ID"/>
<many-to-many
class="cfc:platform.shared.models.entities.Episode" column="ASSET_ID"/>
</bag>
<!-- This is the new relationship causing trouble -->
<bag lazy="extra" name="Products" table="PRESENTERS_TO_PRODUCTS">
<cache region="ATVEntityCache" usage="read-only"/>
<key column="PRESENTER_ID"/>
<many-to-many
class="cfc:platform.shared.models.entities.Product"
column="PRODUCT_ID" not-found="ignore"/>
</bag>
<!-- End new relationship -->
<many-to-one
class="cfc:platform.shared.models.entities.AttachmentImage"
column="presenter_photo" lazy="proxy" name="Photo"/>
</class>
</hibernate-mapping>
非常感谢。
**** 更新 1 **** 我添加了 inversejoincolumn 列。我可以使用以下断言访问单元测试中的产品,但是,当我转储实体时,错误会自行显示。
var p = entityLoadByPK('Presenter',276);
var pr = p.getProducts();
// Works
assertEquals(2,arrayLen(pr),'Expected 2 products');
// Also works
assertEquals('ProductA',pr[1].getproduct_name(),'Incorrect product name.');
// Also works
assertEquals('ProductB',pr[2].getproduct_name(),'Incorrect product name.');
// Fails
writeDump(var=p,top=2,abort=true);
**** 更新 2 **** 我已授予解决方案;然而,有些事情仍然没有解决。我附上了显示剩余问题的转储屏幕截图。 ColdFusion 说“[未定义的数组元素] 无法检索集合大小:[Product.Presenters#0]”。
I am running ColdFusion 9.01 with all the latest hotfixes applied as of this writing. I've been using ColdFusion 9's Hibernate integration on a production site for well over a year and now need to relate two existing entities; however, Hibernate is generating incorrect column names for a many-to-many relationship.
My two existing entities are Presenter and Product. I have dbcreate set to "none" and am using the default values for autogenmap on a Oracle 11g database. So, Hibernate is not responsible for schema changes to my database.
Here is the new property I added to my Presenter entity.
/** Relation to any episodes associated to this presenter. **/
property name='Products' fieldtype='many-to-many' lazy='extra'
singularName="Product" cfc='platform.shared.models.entities.Product' fkcolumn='PRESENTER_ID'
linktable='PRESENTERS_TO_PRODUCTS' cacheuse = 'read-only' cachename = 'EntityCache' inversejoincolumn='PRODUCT_ID';
Here is the invalid SQL generated by Hibernate.
select count(Product_product_id) from PRESENTERS_TO_PRODUCTS where Presenter_presenter_id =?
I am unsure why Hibernate is prefixing the columns with the name of the entity. The SQL should look like this:
select count(product_id) from PRESENTERS_TO_PRODUCTS where presenter_id =?
As I diagnose this, I saved the HBXML files generated by Hibernate. I've included the full file for my Presenter entity below.
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="Presenter" lazy="true"
name="cfc:platform.shared.models.entities.Presenter" table="PRESENTERS">
<id name="presenter_id" type="integer">
<column name="presenter_id"/>
<generator class="increment"/>
</id>
<property name="presenter_bio" type="text">
<column name="presenter_bio"/>
</property>
<property name="presenter_name" type="text">
<column name="presenter_name"/>
</property>
<property name="presenter_website" type="text">
<column name="presenter_website"/>
</property>
<property name="presenter_company" type="text">
<column name="presenter_company"/>
</property>
<property name="presenter_internalname" type="text">
<column name="presenter_internalname"/>
</property>
<property name="presenter_logo" type="integer">
<column name="presenter_logo"/>
</property>
<property name="presenter_description" type="text">
<column name="presenter_description"/>
</property>
<property name="deleted" type="integer">
<column name="deleted"/>
</property>
<property name="presenter_is_evangelist" type="integer">
<column name="presenter_is_evangelist"/>
</property>
<property name="presenter_is_expert" type="integer">
<column name="presenter_is_expert"/>
</property>
<property name="presenter_about" type="text">
<column name="presenter_about"/>
</property>
<bag lazy="extra" name="Episodes" table="ASSET_TO_PRESENTERS">
<cache region="EntityCache" usage="read-only"/>
<key column="PRESENTER_ID"/>
<many-to-many
class="cfc:platform.shared.models.entities.Episode" column="ASSET_ID"/>
</bag>
<!-- This is the new relationship causing trouble -->
<bag lazy="extra" name="Products" table="PRESENTERS_TO_PRODUCTS">
<cache region="ATVEntityCache" usage="read-only"/>
<key column="PRESENTER_ID"/>
<many-to-many
class="cfc:platform.shared.models.entities.Product"
column="PRODUCT_ID" not-found="ignore"/>
</bag>
<!-- End new relationship -->
<many-to-one
class="cfc:platform.shared.models.entities.AttachmentImage"
column="presenter_photo" lazy="proxy" name="Photo"/>
</class>
</hibernate-mapping>
Much appreciated.
**** UPDATE 1 ****
I've added the inversejoincolumn column. I am able to access the products in my unit test with the following assertions, however, when I dump the entity, the error shows itself.
var p = entityLoadByPK('Presenter',276);
var pr = p.getProducts();
// Works
assertEquals(2,arrayLen(pr),'Expected 2 products');
// Also works
assertEquals('ProductA',pr[1].getproduct_name(),'Incorrect product name.');
// Also works
assertEquals('ProductB',pr[2].getproduct_name(),'Incorrect product name.');
// Fails
writeDump(var=p,top=2,abort=true);
**** UPDATE 2 ****
I've awarded the solution; however, something is still off. I've attached a screenshot of the DUMP showing the remaining issue. ColdFusion says "[undefined array element] could not retrieve collection size: [Product.Presenters#0]".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当我使用多对多关系时,我总是有一个 inversejoincolumn 属性。我认为这只是猜测,因为它没有明确定义。我的人际关系通常是这样的:
Whenever I use a many to many relationship, i always have a inversejoincolumn attribute. I think it is just guessing it because it is not defined explicitly. Here are what my relationships always look like: