哪些ORM能很好地与Scala一起工作?[英] What ORMs work well with Scala?

本文是小编为大家收集整理的关于哪些ORM能很好地与Scala一起工作?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我将要编写一个依赖MySQL数据库的Scala命令行应用程序.我一直在寻找ORM,并且很难找到可以很好地工作的OR.

举起ORM 看起来不错,但我不确定它是否可以与整个升降网络框架中解耦. ActiveObjects看起来还不错,但作者说它可能与Scala无法正常工作.

我不是从Java来到Scala,所以我不知道所有选项.有没有人使用Scala的ORM,如果是的话,您使用了什么以及它的工作方式?

推荐答案

有几个原因是,以JPA为导向的框架(例如Hibernate)不适合惯用的Scala应用程序:

  • 没有嵌套注释为 scala 2.8> scala 2.8 preview - 这意味着您意味着您不能将注释用作复杂应用程序的映射元数据(即使是最简单的应用程序,也经常使用@JoinTable - > @JoinColumn);
  • Scala和Java收集之间的不一致性使开发人员转换了集合;在某些情况下,如果不实施基础框架的复杂接口(例如,Hibernate's PersistentCollections),不可能将Scala集合映射到关联.
  • 一些非常常见的功能,例如域模型验证,需要持续类上的Javabeans约定 - 这些东西并不是做事的" Scala方式";
  • 当然,Interop问题(例如原始类型或代理)引入了一个全新的问题,这些问题无法轻易四处走动.

我敢肯定,还有更多原因.这就是为什么我们开始启动 carlflex orm project .这种纯萨拉奥(Pure-Scala Orm)试图消除经典Java Orms的噩梦.具体来说,您几乎以经典的DDL语句来定义您的实体:

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 Pojos多一些.但是实际上,有几个概念聚集在一起:

  • 用于生成模式的精确DDL(您可以轻松地以类似于DSL的方式添加索引,外键和其他内容);
  • 所有查询都可以在"表对象"中组装在一起,而不是散布在Dao中.查询本身是非常灵活的,您可以在变量中存储查询对象,谓词,预测,子查询和关系别名,因此您可以重复使用它们,甚至可以从现有查询中进行批处理更新操作(插入求解求解程序(insert-seertsect)示例);
  • 可以通过懒惰或渴望获得策略来实现关联之间(一对一,一对一,一对一,一对一和多次交流中间关系)之间的透明导航;在这两种情况下
  • 验证是框架的一部分;
  • 还有一个Maven2插件,允许生成模式并从方便的XML格式化文件中导入初始数据.

唯一缺乏的bullflex orm是:

  • 多列主要键(尽管可以创建以多列唯一约束为支持的多列外键,但仅用于数据完整性);
  • 完整的文档(尽管我们正在积极地进行研究);
  • 具有绕过核心技术的一亿美元生产系统的成功故事.

P.S.我希望这篇文章不会被视为广告.确实不是这样 - 我试图尽可能地客观.

其他推荐答案

我尝试了 eclipselink jpa 和基本操作对我来说很好. JPA是JAVA标准,还有其他实现也可能起作用( openjpa 等).这是Scala中JPA类的示例:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity { val name = "Users" }
class User {
  @Id
  @GeneratedValue
  var userid:Long = _

  var login:String = _

  var password:String = _

  var firstName:String = _

  var lastName:String = _
}

其他推荐答案

Slick 是功能性世界的完美匹配.传统ORM并不是Scala的完美合适. Slick很好地组成,并使用模仿Scala收集类和综合的DSL.

本文地址:https://www.itbaoku.cn/post/597431.html

问题描述

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.

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.

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?

推荐答案

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

  • 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.

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]

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:

  • 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.

The only things Circumflex ORM lacks are:

  • multi-column primary keys (although it is possible to create multi-column foreign keys backed by multi-column unique constraints, but it is only for data integrity);
  • full-fledged documentation (although we are actively working on it);
  • success stories of ten-billion-dollar production systems that have Circumflex ORM as it's core technology.

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.

其他推荐答案

I experimented with EclipseLink JPA and basic operations worked fine for me. JPA is a Java standard and there are other implementations that may also work (OpenJPA, etc). Here is an example of what a JPA class in Scala looks like:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity { val name = "Users" }
class User {
  @Id
  @GeneratedValue
  var userid:Long = _

  var login:String = _

  var password:String = _

  var firstName:String = _

  var lastName:String = _
}

其他推荐答案

Slick is a perfect match for a functional world. Traditional ORM's are not a perfect fit for Scala. Slick composes well and uses a DSL that mimics Scala collection classes and for comprehensions.