Question

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

Was it helpful?

Solution

I tend to only store the ID of the user that is logged in in the session.

Then I have a UserService.getCurrentUser() facade method that returns that user if I need it.

That way the user is always current, and never detached.

OTHER TIPS

I generally just use a lightweight proxy object (containing top-level properties only) in session and only load the full entity as needed in exactly the same use case as you've described. Don't use method #1 unless you really want to burn yourself (experience talking there).

Hibernate sessions are lazy loaded, and do not persist. So while you have the CF object in memory, they're pointing to a Hibernate session which is out of scope, for lack of better terminology. To get back in scope, you basically need to wake it up on your subsequent requests, using something like EntitySave() or EntityLoadByExample()

I do agree that wrapping in a service not only helps you avoid some of these issues, but is overall better architecturally than touching the entity directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top