ColdFusion 9 ORM problem with mapping a FK to the same entity
Tags: ColdFusion 9, CFORM
After seeing a question on the cf-orm developers mailing list, I thought I would investigate the problem a little further. It turns out that something so basic has not been implemented correctly, or ColdFusion has made it a bit harder than one should need to think about the problem.
I only have one work around for this which I have added at the end of this post.
So lets have a look at the problem, in creating a Nested Set List I was looking at the following code.
property name="categoryId" generator="identity" fieldtype="id";
property name="left" column="leftId";
property name="right" column="rightId";
property name="name" ormtype="string" length="255";
property name="description" ormtype="text";
property type="timestamp" name="created" generated="insert";
property type="timestamp" name="modified" generated="always";
}
Now this is very a basic ORM mapping, and the one thing left to do is create a parent for the Nested List. In hibernate this is as easy as adding a many to one relationship, but I found that it doesn't work the way one would hope that it would work.
In ColdFusion I would need to add the following line, at least that is what one would expect to do.
Now what I would expect from adding this line is that it added a mapping for parentId, that was mapped to itself to give a different property. However in ColdFusion this is not the case, and throws an error.
This is achieved by switching on the save mapping in the Application.cfc and then inspecting the generated hbmxml file, which looks like this.
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="forum" lazy="true"
name="cfc:domains.category" table="forum">
<id name="categoryId" type="int">
<column length="10" name="categoryId"/>
<generator class="identity"/>
</id>
<property name="left" type="string">
<column name="leftId"/>
</property>
<property name="right" type="string">
<column name="rightId"/>
</property>
<property name="name" type="string">
<column length="255" name="name"/>
</property>
<property name="description" type="text">
<column length="255" name="description"/>
</property>
<property generated="insert" name="created" type="java.sql.Timestamp">
<column name="created"/>
</property>
<property generated="always" name="modified" type="java.sql.Timestamp">
<column name="modified"/>
</property>
<many-to-one class="cfc:domains.category"
column="categoryId" name="parent"/>
</class>
</hibernate-mapping>
</code>
Now even though I specified the column attribute in my original entity, it seems that this is not translated across to the hbmxml file. So the only way to get this to work in ColdFusion I am forced to keep the hbmxml file and modify it to the following, and run the application again. And this time it works as expected.
<!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="forum" lazy="true"
name="cfc:domains.category" table="forum">
<id name="categoryId" type="int">
<column length="10" name="categoryId"/>
<generator class="identity"/>
</id>
<property name="left" type="string">
<column name="leftId"/>
</property>
<property name="right" type="string">
<column name="rightId"/>
</property>
<property name="name" type="string">
<column length="255" name="name"/>
</property>
<property name="description" type="text">
<column length="255" name="description"/>
</property>
<property generated="insert" name="created" type="java.sql.Timestamp">
<column name="created"/>
</property>
<property generated="always" name="modified" type="java.sql.Timestamp">
<column name="modified"/>
</property>
<many-to-one class="cfc:domains.category"
column="parentId" name="parent"/>
</class>
</hibernate-mapping>
So the problem is why can I make this change manually, and yet I am not able to tell the column to be as described in the ColdFusion Entity code?
There are no comments for this entry.



TweetBacks