如何将会话/事务获取封装到Squeryl中关系的lazy-init中?

发布于 2024-10-15 03:13:26 字数 2006 浏览 1 评论 0原文

我正在尝试使用 Squeryl 实现一对多关系,并遵循其网站上的说明

该文档给出了以下示例:

object SchoolDb extends Schema {    
  val courses = table[Course]    
  val subjects = table[Subject]      
  val subjectToCourses =
    oneToManyRelation(subjects, courses).
    via((s,c) => s.id === c.subjectId)  
}

class Course(val subjectId: Long) extends SchoolDb2Object {    
  lazy val subject: ManyToOne[Subject] = SchoolDb.subjectToCourses.right(this)
}

class Subject(val name: String) extends SchoolDb2Object {    
  lazy val courses: OneToMany[Course] = SchoolDb.subjectToCourses.left(this)
}

我发现对 Course.subjectSubject.courses 的任何调用都需要包装在事务中。然而,我使用 ORM 的目标之一是向调用者隐藏这些详细信息。因此,我不希望调用代码必须将对这些字段的调用包装在事务中。

看来,如果我修改示例以将惰性初始化函数包装在事务中,如下所示:

class Subject(val name: String) extends SchoolDb2Object {    
  lazy val courses: OneToMany[Course] = {
    inTransaction {
      SchoolDb.subjectToCourses.left(this)
    }
}

我收到以下异常:

Exception in thread "main" java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create 
and bound to the thread via 'work' or 'bindToCurrentThread'
    at scala.Predef$.error(Predef.scala:58)
    at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:111)
    at org.squeryl.Session$$anonfun$currentSession$1.apply(Session.scala:111)
    at scala.Option.getOrElse(Option.scala:104)
    at org.squeryl.Session$.currentSession(Session.scala:110)
    at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$$_dbAdapter(AbstractQuery.scala:116)
    at org.squeryl.dsl.AbstractQuery$$anon$1.<init>(AbstractQuery.scala:120)
    at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:118)
    at org.squeryl.dsl.DelegateQuery.iterator(DelegateQuery.scala:9)

但是,正如我所说,如果我将调用者包装在事务中,那么一切都会正常。

那么,我如何封装该对象由对象本身的数据库支持的事实呢?

I am trying to implement a One-To-Many relation using Squeryl, and following the instructions on their site.

The documentation gives the following example:

object SchoolDb extends Schema {    
  val courses = table[Course]    
  val subjects = table[Subject]      
  val subjectToCourses =
    oneToManyRelation(subjects, courses).
    via((s,c) => s.id === c.subjectId)  
}

class Course(val subjectId: Long) extends SchoolDb2Object {    
  lazy val subject: ManyToOne[Subject] = SchoolDb.subjectToCourses.right(this)
}

class Subject(val name: String) extends SchoolDb2Object {    
  lazy val courses: OneToMany[Course] = SchoolDb.subjectToCourses.left(this)
}

I find that any calls to Course.subject or Subject.courses needs to be wrapped in a transaction. However, One of my goals in using an ORM is to hide these details from callers. As such, I don't want the calling code to have to wrap a call to these fields in a transaction.

It seems that if I modify the example to wrap the lazy init function in a transaction, like so:

class Subject(val name: String) extends SchoolDb2Object {    
  lazy val courses: OneToMany[Course] = {
    inTransaction {
      SchoolDb.subjectToCourses.left(this)
    }
}

I get the following exception:

Exception in thread "main" java.lang.RuntimeException: no session is bound to current thread, a session must be created via Session.create 
and bound to the thread via 'work' or 'bindToCurrentThread'
    at scala.Predef$.error(Predef.scala:58)
    at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:111)
    at org.squeryl.Session$anonfun$currentSession$1.apply(Session.scala:111)
    at scala.Option.getOrElse(Option.scala:104)
    at org.squeryl.Session$.currentSession(Session.scala:110)
    at org.squeryl.dsl.AbstractQuery.org$squeryl$dsl$AbstractQuery$_dbAdapter(AbstractQuery.scala:116)
    at org.squeryl.dsl.AbstractQuery$anon$1.<init>(AbstractQuery.scala:120)
    at org.squeryl.dsl.AbstractQuery.iterator(AbstractQuery.scala:118)
    at org.squeryl.dsl.DelegateQuery.iterator(DelegateQuery.scala:9)

But, like I said, if I wrap the caller in a transaction, then everything works.

So, how can I encapsulate the fact that this object is backed by a database in the object itself?

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

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

发布评论

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

评论(1

笑梦风尘 2024-10-22 03:13:26

我假设您在调用课程对象时遇到此错误?

我不太了解 Squeryl 的工作原理,但我相信 OneToMany[Course] 是一个活动对象。这意味着对课程对象的调用需要一个会话,因为任何调用都可能会延迟地访问数据库来获取数据。

如何组织它取决于您使用的应用程序类型。在 Web 应用程序中,添加过滤器(第一个入口点)来启动和停止事务通常是有意义的。在 GUI 客户端(例如 swing 应用程序)中,在收到用户交互时启动事务是一个很好的解决方案。这样,您就可以获得不太长的事务,并且还可以扩展您希望以原子方式执行的调用(无论是完全执行还是根本不执行)。

I assume you get this error in calls on the courses object?

I don't know very much about how Squeryl works, but I believe that the OneToMany[Course] is a live object. That means that the calls on the courses object need a session since any call may lazily go to the database to fetch data.

How you organise this depends on what type of application you use. In a web application it often makes sense to add a filter (first point of entry) to start and stop the transaction. In a GUI client, say a swing application, it's a good solution to start the transaction at the point where you receive the user interaction. That way you get transactions that are not to long and also stretches over calls which you expect to be performed atomically (either fully or not at all).

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