Hello,
Does anyone has experience in using EntityMerge and EntitySave.
The question is simple, why should we use EntitySave and not always
EntityMerge ?
My components are simple:
- user has unidirectional many-to-one relation with company.
- group has unidirectional one-to-many relation with company.
- group has an array collection users: a many-to-many relation with
user
As you can see I don't use bidirectional relations.
---------------------------- CFCs BEGIN
----------------------------
component persistent="true" table="um_group" output="false"
{
/* PROPERTIES */
property name="grp_id" column="grp_id" type="numeric" ormtype="int"
fieldtype="id" generator="native";
property name="grp_code" column="grp_code" type="string"
ormtype="string";
property name="grp_name" column="grp_name" type="string"
ormtype="string";
property name="grp_desc" column="grp_desc" type="string"
ormtype="string";
property name="grp_status" column="grp_status" type="numeric"
ormtype="short";
property name="grp_sys" column="grp_sys" type="numeric"
ormtype="byte";
/* RELATIONS */
property name="users" fieldtype="many-to-many" cfc="um_user"
fkColumn="grp_id"
linktable="um_user_group_link" InverseJoinColumn="usr_id"
type="array" lazy="true" cascade="save-update"
notnull="false"
remotingfetch="true" singularname="user"
default="[]";
/* FUNCTIONS */
public void function setUsers(required array newUsers)
{
variables.users = [];
if (arraylen(arguments.newUsers) gt 0){
for (i = 1; i <= arraylen(arguments.newUsers); i++ ){
newUser = arguments.newUsers[i];
this.addUser(newUser);
}
}
}
component persistent="true" table="um_user" output="false"
{
/* PROPERTIES */
property name="usr_id" column="usr_id" type="numeric" ormtype="int"
fieldtype="id" generator="native";
property name="usr_email" column="usr_email" type="string"
ormtype="string";
property name="usr_pwd" column="usr_pwd" type="string"
ormtype="string";
property name="usr_salt" column="usr_salt" type="string"
ormtype="string" insert="false" update="false";
property name="usr_first_name" column="usr_first_name" type="string"
ormtype="string";
property name="usr_last_name" column="usr_last_name" type="string"
ormtype="string";
property name="usr_status" column="usr_status" type="numeric"
ormtype="short";
property name="usr_job_title" column="usr_job_title" type="string"
ormtype="string";
property name="usr_phone" column="usr_phone" type="string"
ormtype="string";
property name="usr_fax" column="usr_fax" type="string"
ormtype="string";
property name="usr_mobile" column="usr_mobile" type="string"
ormtype="string";
property name="usr_title" column="usr_title" type="string"
ormtype="string";
/* RELATIONS */
property name="company" fieldtype="many-to-one" fkcolumn="cpy_id"
cfc="um_company"
notnull="false" remotingfetch="true"
inverse="true"
cascade="save-update" lazy="true";
/* FUNCTIONS */
/**@hint Override setCompany to Handle the Null problem with Flex*/
public function setCompany(required any newCompany)
{
if (IsInstanceOf(arguments.newCompany,"um_company")){
variables.company = arguments.newCompany;
//arguments.newCompany.addUser(this); Used only if Bidirectional
}
else{
variables.company = JavaCast("Null", "");//If Null is acceptable,
if not, set a default company.
}
}
component persistent="true" table="um_company" output="false"
{
/* PROPERTIES */
property name="cpy_id" column="cpy_id" type="numeric" ormtype="int"
fieldtype="id" generator="native";
property name="cpy_code" column="cpy_code" type="string"
ormtype="string" length="10";
property name="cpy_name" column="cpy_name" type="string"
ormtype="string" length="50";
/* RELATIONS */
property name="groups" fieldtype="one-to-many" fkColumn="cpy_id"
cfc="um_group"
type="array" lazy="true" singularname="group"
remotingfetch="true" cascade="all";
---------------------------- CFCs END ----------------------------
From Flex, I load a group with his collection of users, and add a new
user to the group, then call an update function:
remote um_group function updateum_group(um_group item)
{
entitysave(item);
return item;
}
And got the following error message:
Unable to invoke CFC - a different object with the same identifier
value was already associated with the session: [um_user#5]
Root cause :org.hibernate.NonUniqueObjectException: a different object
with the same identifier value was already associated with the
session: [um_user#5]
I solve the problem and my object get persisted in the database by
changing my update function to :
remote um_group function updateum_group(um_group item)
{
entitymerge(item);
return item;
}
So why should we use EntitySave and not always EntityMerge ?
Thanks,
Simon
--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To post to this group, send email to
cf-or...@googlegroups.com.
To unsubscribe from this group, send email to
cf-orm-dev+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/cf-orm-dev?hl=en.