如何在 CF9 中使用分离对象?
如果 User
是一个实体,并且我需要将 User
存储在 Session
中,它将在下一个请求时分离。
AFAIK 只有 2 种方法来处理这些分离的对象
EntityMerge(session.user)
- 使用会话的对象更新数据库(不安全)session.userID
-entityLoadByPK( )
在下一个请求时再次(更多负载)
这是唯一的两个解决方法吗?还有其他办法吗?
根据 ColdFusion 9 ORM 集成的高级技术幻灯片 如果实体在合并时发生更改,则方法 #1 的并发将引发错误,但这有什么用呢?捕获异常并使用方法#2?
何时使用EntityReload()?我认为它的工作方式与 EntityMerge(entity) 相同,但事实并非如此。
谢谢!
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
EntityMerge(session.user)
- update DB with session's object (unsafe)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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我倾向于只存储会话中登录的用户的 ID。
然后,我有一个 UserService.getCurrentUser() 外观方法,如果需要,它会返回该用户。
这样,用户始终处于当前状态,并且永远不会脱离。
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.
我通常只在会话中使用轻量级代理对象(仅包含顶级属性),并且仅在与您所描述的完全相同的用例中根据需要加载完整实体。不要使用方法#1,除非你真的想烧伤自己(体验在那里说话)。
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 会话是延迟加载的,并且不会持久。因此,虽然内存中有 CF 对象,但由于缺乏更好的术语,它们指向超出范围的 Hibernate 会话。要回到范围内,您基本上需要在后续请求中唤醒它,使用 EntitySave() 或 EntityLoadByExample() 之类的东西
我确实同意,包装在服务中不仅可以帮助您避免其中一些问题,而且总体上会更好建筑上比直接接触实体更重要。
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.