org.hibernate. NonUniqueObjectException: a different object with the same identifier value Part 2

Let's have another look at this and see if we can change our logic up to eliminate the use of EntityLoadByExample. This wasn't working consistently across all similar functions in the application. This method will allow us to eliminate the need for ORMGetSession().merge() methods, or ORMEntityMerge() methods.

We will check to see if an object is returned from EntityLoad using our primary keys.

Here's the code walk through:

First we set all our variables from arguments or ORM defined objects.

view plain print about
1var ReferralInfoArray = getCompany().getCompanyInfo().getReferralInfo();
2var CompanyCode = getCompany().getCompanyCode();
3var ReferralId = arguments.eventArgs.ReferralId;
4var ReferralSource = arguments.eventArgs.ReferralSource;
5var ReferralEmail = arguments.eventArgs.ReferralEmail;

Next we create or ORM Entity to utilize the methods and attempt to create the object that contains the primary keys for which we are looking.

view plain print about
1var ReferralSourceArray = application.orm.createEntity("COMPANY_REFERRAL_INFO");
2var objReferralSource = ReferralSourceArray.EntityLoad(CompanyCode=CompanyCode,ReferralId=ReferralId);

If an object is returned with our primary keys, then we know it already exists in the ORM session. So we test to see if it is an object, and if so, set the other variables.

view plain print about
1if (isObject(objReferralSource)){
2        objReferralSource.setEmailAddress(ReferralEmail);
3        objReferralSource.setSource(ReferralSource);
4    }

If an object is not returned, this means that this object does not exist in the ORM session. So we set the variables using the original ORM Entity methods and save. We have to save because it's new and not in the ORM session.

view plain print about
1else {
2        ReferralSourceArray.setCompanyCode(CompanyCode);
3        ReferralSourceArray.setEmailAddress(ReferralEmail);
4        ReferralSourceArray.setSource(ReferralSource);
5        ReferralSourceArray.setReferralId(ReferralId);
6        ReferralSourceArray.save(ReferralSourceArray);
7    }

And there we go.....

Here's the whole function for reference:

view plain print about
1<cffunction name="submitReferralSource" access="public" output="false" hint="uses the cust admin service to add/edit referral source info">
2 <cfargument name="eventArgs" required="true" />
3 <cfscript>
4var ReferralInfoArray = getCompany().getCompanyInfo().getReferralInfo();
5var CompanyCode = getCompany().getCompanyCode();
6var ReferralId = arguments.eventArgs.ReferralId;
7var ReferralSource = arguments.eventArgs.ReferralSource;
8var ReferralEmail = arguments.eventArgs.ReferralEmail;
9var ReferralSourceArray = application.orm.createEntity("COMPANY_REFERRAL_INFO");
10var objReferralSource = ReferralSourceArray.EntityLoad(CompanyCode=CompanyCode,ReferralId=ReferralId);
11    
12    if isObjectt(objReferralSource)){
13        objReferralSource.setEmailAddress(ReferralEmail);
14        objReferralSource.setSource(ReferralSource);
15    }
16 else {
17        ReferralSourceArray.setCompanyCode(CompanyCode);
18        ReferralSourceArray.setEmailAddress(ReferralEmail);
19        ReferralSourceArray.setSource(ReferralSource);
20        ReferralSourceArray.setReferralId(ReferralId);
21        ReferralSourceArray.save(ReferralSourceArray);
22    }
23
</cfscript>
24</cffunction>

org.hibernate. NonUniqueObjectException: a different object with the same identifier value

Message
a different object with the same identifier value was already associated with the session: [COMPANY_REFERRAL_INFO#{CompanyCode=1002, ReferralId=10}]

Detail
Root cause :org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [COMPANY_REFERRAL_INFO#{CompanyCode=1002, ReferralId=10}]

ColdFusion 9 ORM. This error occurs when using the same function to update and create records. The save works fine for new records but update an existing record throws the error above.

Here's what works for saving the new record:

view plain print about
1private.ReferralSourceArray.save(private.ReferralSourceArray);

ORM will not allow us to update an object with the same ID already defined in the session. Doing a little research gave us the merge method. This works for saving updated records, however, not for saving newly created records.

Here's what works for saving an updated record:

view plain print about
1obj = ORMGetSession().merge(private.ReferralSourceArray);
2private.ReferralSourceArray.save(obj);

So how do we use both? Creating an if statement, but how to differentiate? EntityLoadByExample!

view plain print about
1if (ArrayLen(EntityLoadByExample(private.ReferralSourceArray))){
2    obj = ORMGetSession().merge(private.ReferralSourceArray);
3    private.ReferralSourceArray.save(obj);
4}
5else {
6    private.ReferralSourceArray.save(private.ReferralSourceArray);
7}

EntityLoadByExample loads and returns an array of objects that match the sample entity. If none exists, it returns an empty array. This is how we know if the record to be changed is new or an update. It checks the record to be changed (it's passed in) against the existing ORM records.

Here's the whole function for reference:

view plain print about
1<cffunction name="submitReferralSource" access="public" output="false" hint="uses the cust admin service to add/edit referral source info">
2    <cfargument name="eventArgs" required="true" />
3    <cfscript>
4    var ReferralInfoArray = getCompany().getCompanyInfo().getReferralInfo();
5    var CompanyCode = getCompany().getCompanyCode();
6    var private = structNew();
7    private.ReferralSourceArray = application.orm.createEntity("COMPANY_REFERRAL_INFO");
8private.ReferralSourceArray.setCompanyCode(CompanyCode);
9private.ReferralSourceArray.setEmailAddress(arguments.eventArgs.ReferralEmail);
10private.ReferralSourceArray.setSource(arguments.eventArgs.ReferralSource);
11private.ReferralSourceArray.setReferralId(arguments.eventArgs.ReferralId);
12    if ArrayLenn(EntityLoadByExample(private.ReferralSourceArray))){
13        obj = ORMGetSession().merge(private.ReferralSourceArray);
14        private.ReferralSourceArray.save(obj);
15    }
16    else {
17private.ReferralSourceArray.save(private.ReferralSourceArray);
18    }
19    
</cfscript>
20</cffunction>

BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner