How to work with detached object in CF9.0.1?

27 views
Skip to first unread message

Henry Ho

unread,
Dec 13, 2010, 9:53:56 PM12/13/10
to cf-orm-dev
If User is an entity, and I need to store User in Session, it will be
detached on next request.

AFAIK there're only 2 methods to handle these detached objects

1.) EntityMerge(session.user) - update DB with session's object
(unsafe)

2.) session.userID - entityLoadByPK() again on next request (more
load)

Are these the only 2 workarounds? Any other ways?

According to Advanced Techniques with ColdFusion 9 ORM Integration
Slide Deck (http://www.silverwareconsulting.com/index.cfm/2009/10/19/
Advanced-Techniques-with-ColdFusion-9-ORM-Integration--Slides-
Available ) Concurrency - with method #1 will throw error if entity
has been changed on merge, but how is this useful? catch the exception
and use method #2?

When to use EntityReload()? I thought it works the same way as
EntityMerge(entity) but it doesn't.

Thanks!


p.s. I've also asked at: http://stackoverflow.com/questions/4435377/how-to-work-with-detached-object-in-cf9

Brian Kotek

unread,
Dec 13, 2010, 10:28:49 PM12/13/10
to cf-or...@googlegroups.com
Store the ID and load it when you need it.


--
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.


Bob Silverberg

unread,
Dec 13, 2010, 10:30:35 PM12/13/10
to cf-or...@googlegroups.com
I advise folks to take approach #2 and store just the PK in the
session scope, and load the entity as needed. If you need more
properties then store then as additional keys in a struct, or store an
entire object, just not a persistent object.

In terms of how to deal with detached objects, you can actually call
both enitySave() and entityMerge() on a detached object to put it back
into the current Hibernate session. entitySave() will sometimes throw
an error, however (if the same object is already loaded into the
session), and entityMerge() won't work with transient objects. These
are just some of the reasons why avoiding detached objects is the best
approach imo.

Regarding the concurrency issue, yes, if you have concurrency checking
turned on (which most people do not), if you try to merge a stale
object you'll get an exception, which is probably what you want, as
you've got concurrency checking turned on.

I hope that helps,
Bob

On Mon, Dec 13, 2010 at 9:53 PM, Henry Ho <henry...@gmail.com> wrote:

> --
> 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.
>
>

--
Bob Silverberg
www.silverwareconsulting.com

Reply all
Reply to author
Forward
0 new messages