如何在 CF9 中使用分离对象?

发布于 2024-10-07 23:02:22 字数 659 浏览 0 评论 0原文

如果 User 是一个实体,并且我需要将 User 存储在 Session 中,它将在下一个请求时分离。

AFAIK 只有 2 种方法来处理这些分离的对象

  1. EntityMerge(session.user) - 使用会话的对象更新数据库(不安全)
  2. 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

  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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人事已非 2024-10-14 23:02:22

我倾向于只存储会话中登录的用户的 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.

遥远的她 2024-10-14 23:02:22

我通常只在会话中使用轻量级代理对象(仅包含顶级属性),并且仅在与您所描述的完全相同的用例中根据需要加载完整实体。不要使用方法#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).

触ぅ动初心 2024-10-14 23:02:22

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文