views:

249

answers:

1

Does anyone know about this error I get when I try to insert a foreign key value in my entry entity using ColdFusion 9 Hibernate integration?

java.lang.ClassCastException: java.lang.String cannot be cast to coldfusion.cfc.CFCBeanProxy

Root cause :org.hibernate.HibernateException: java.lang.ClassCastException: java.lang.String cannot be cast to coldfusion.cfc.CFCBeanProxy

Below is the code for my entity object and then for my user object..

Is there anything wrong with this?

entry.cfc

/**
* Entries Object
*/
component output="false" persistent="true"{
    property name="entry_id" fieldType="id" generator="uuid";
    property name="entryBody" ormType="text";
    property name="title" notnull="true" type="string";
    property name="time" fieldtype="timestamp";
    property name="isCompleted" ormType="boolean" dbdefault="0" default="false";
    property name="userID" fieldtype="many-to-one" fkcolumn="userID" cfc="user";

    Entry function init() output=false{
        return this;
    }
}

user.cfc

/**
* Users Object
*/
component output="false" persistent="true"{
    property name="userID" fieldType="id" generator="uuid";
    property name="firstName" notnull="true" type="string";
    property name="lastName" notnull="true" type="string";
    property name="password" notnull="true" type="string";
    property name="userType" notnull="true" type="string";
    //property name="entry" fieldtype="one-to-many" type="array" fkcolumn="userID" cfc="entry";

    User function init() output=false{
        return this;
    }
}
+1  A: 
property name="user" type="User" fieldtype="many-to-one" fkcolumn="userID" cfc="User";

Because the property should be type User, not the ID? In CF-ORM/Hibernate, there's no way to just set FK. You must...

entry = EntityNew("Entry");
user = EntityLoadByPK("user",1);
entry.setUser(user);
Henry
Thanks for that Henry I get I get this different error message now:Error Type: ColdBox.plugins.BeanFactory.PopulateBeanException : [N/A]Error Messages: Error populating bean guestbook.model.entry with argument USER of type class java.lang.String.If the component name is specified as a type of this argument, it is possible that either a definition file for the component cannot be found or is not accessible.The user argument passed to the SETUSER function is not of type user.
faheem
that means, you should remove user (it is really userID:String) from coldbox's RC. Then set User yourself after populate is called.
Henry
sorry to ask but where do i remove the user from ?
faheem
getPlugin("beanFactory").populateBean() populate the bean from RC (i.e. FORM + URL scope). So since your rc.user is currently a string that contains userID, and populateBean() is calling setUser(rc.user) which caused the exception.
Henry
Either: rename your view/form to use userID instead of user, so populateBean() will not complain. Or, backup the rc.user as userID, delete from rc, before you call populateBean(). Then finally: entry.setUser(entityLoadByPK("user", userID));
Henry
I had a friend of mine help me following your example and it worked thank you very much.
faheem
great, then you should marked this answered.
Henry
thanks do you know where is the option for me to mark this as answered ?
faheem
Look for the check mark beside the Vote of this answer. :)
Henry