什么ORM与Scala工作良好? [英] What ORMs work well with Scala?

查看:110
本文介绍了什么ORM与Scala工作良好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要写一个依赖于MySQL数据库的Scala命令行应用程序。

I'm about to write a Scala command-line application that relies on a MySQL database. I've been looking around for ORMs, and am having trouble finding one that will work well.

Lift ORM 看起来不错,但我不确定它可以从整个Lift网络框架中解耦。 ActiveObjects也看起来不错,但作者说,它可能不能很好地与Scala工作。

The Lift ORM looks nice, but I'm not sure it can be decoupled from the entire Lift web framework. ActiveObjects also looks OK, but the author says that it may not work well with Scala.

我不会从Java来到Scala,所以我不知道所有选项。有没有人使用ORM与Scala,如果是,你使用什么和它是如何工作?

I'm not coming to Scala from Java, so I don't know all the options. Has anyone used an ORM with Scala, and if so, what did you use and how well did it work?

推荐答案

几个原因为什么面向JPA的框架(例如Hibernate)不适合成熟的Scala应用程序优雅:

There are several reasons why JPA-oriented frameworks (Hibernate, for instance) do not fit into idiomatic Scala applications elegantly:


  • 没有嵌套注释陈述 Scala 2.8 Preview - 这意味着您不能将注释用作复杂应用程序的映射元数据(即使最简单的常用 @JoinTable - > @JoinColumn );

  • Scala和Java集合之间的不一致使开发人员可以转换集合;还有一些情况下不可能将Scala集合映射到关联,而不实现底层框架的复杂接口(例如,Hibernate的 PersistentCollections );

  • 一些非常常见的功能,例如域模型验证,需要JavaBeans关于持久化类的约定 - 这些东西不是相当Scala方式的做事情;


  • there are no nested annotations as states the Scala 2.8 Preview -- that means you cannot use annotations as mapping metadata for complex applications (even the simplest ones often use @JoinTable -> @JoinColumn);
  • inconsistencies between Scala and Java collections make developers convert collections; there are also cases when it is impossible to map Scala collections to associations without implementing complex interfaces of the underlying framework (Hibernate's PersistentCollections, for example);
  • some very common features, such as domain model validation, require JavaBeans conventions on persistent classes -- these stuff is not quite "Scala way" of doing things;
  • of course, the interop problems (like Raw Types or proxies) introduce a whole new level of issues that cannot be walked around easily.

还有更多的问题原因,我敢肯定。这就是为什么我们开始了 Circumflex ORM项目。这个纯Scala ORM尝试最好消除经典Java ORM的恶梦。具体来说,你可以使用传统的DDL语句来定义你的实体:

There are more reasons, I'm sure. That's why we have started the Circumflex ORM project. This pure-Scala ORM tries it's best to eliminate the nightmares of classic Java ORMs. Specifically, you define your entities in pretty much way you would do this with classic DDL statements:

class User extends Record[User] {
  val name = "name".TEXT.NOT_NULL
  val admin = "admin".BOOLEAN.NOT_NULL.DEFAULT('false')
}

object User extends Table[User] {
  def byName(n: String): Seq[User] = criteria.add(this.name LIKE n).list
}

// example with foreign keys:
class Account extends Record[Account] {
  val accountNumber = "acc_number".BIGINT.NOT_NULL
  val user = "user_id".REFERENCES(User).ON_DELETE(CASCADE)
  val amount = "amount".NUMERIC(10,2).NOT_NULL
}

object Account extends Table[Account]

正如你所看到的,这些声明比传统的JPA POJO更加冗长。但实际上有几个概念组合在一起:

As you can see, these declarations are a bit more verbose, than classic JPA POJOs. But in fact there are several concepts that are assembled together:


  • 用于生成模式的精确DDL(您可以轻松地添加索引,

  • 所有查询都可以在table object中组合而不是分散在DAO中;查询本身是非常灵活的,您可以在变量中存储查询对象,谓词,投影,子查询和关系别名,以便可以重用它们,甚至可以从现有查询进行批量更新操作(insert-select for示例);

  • 关联之间的透明导航(一对一,多对一,一对多和多对多到中间关系)通过懒惰或急切取得策略来实现;在这两种情况下,关联建立在基础关系的外键之上;

  • 验证是框架的一部分;

  • Maven2插件,允许生成模式和从方便的XML格式的文件导入初始数据。

  • the precise DDL for generating schema (you can easily add indexes, foreign keys and other stuff in the same DSL-like fashion);
  • all queries can be assembled inside that "table object" instead of being scattered around in DAO; the queries themselves are very flexible, you can store query objects, predicates, projections, subqueries and relation aliases in variables so you can reuse them, and even make batch update operations from existing queries (insert-select for example);
  • transparent navigation between associations (one-to-one, many-to-one, one-to-many and many-to-many-through-intermediate-relation) can be achieved either by lazy or by eager fetching strategies; in both cases the associations are established on top of the foreign keys of underlying relations;
  • validation is the part of framework;
  • there is also a Maven2 plugin that allows generating schema and importing initial data from handy XML formatted files.

Circumflex ORM缺少的只有:

The only things Circumflex ORM lacks are:


  • 多列主键(虽然可以创建由多列唯一约束支持的多列外键,但它仅用于数据诚信);

  • 完整的文档(尽管我们正在积极工作);

  • 100亿美元生产系统的成功案例有Circumflex ORM作为核心技术。

PS我希望这篇文章不会被认为是一个广告。不是这样,真的 - 我试图尽可能客观。

P.S. I hope this post will not be considered an advertisement. It isn't so, really -- I was trying to be as objective as possible.

这篇关于什么ORM与Scala工作良好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆