Spring数据与Scala [英] Spring Data with Scala

查看:155
本文介绍了Spring数据与Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如前面所提到的,不建议在Scala上使用Spring Data或Hibernate,因为它不适用于字节码操作(和某些接口不兼容)。




然而我的问题是,因为可以用SBT并行编写Scala和Java,是不是一个好主意以Java编码我的存储库和服务,然后在我的Scala代码中使用它们?这是否摆脱了字节码约束?



我在这里问这里是因为找不到任何资源。

解决方案

使用Scala for Hibernate没有问题。你甚至不需要使用 @BeanProperty 。例如:

  @Entity 
类角色{
@Id @GeneratedValue
var id :Long = _

@Index(name =role_name)
var name:String = _

var created_at:Date = _
var updated_at:Date = _

@ManyToMany
private var sub_role:java.util.Set [Role] = _
def subRoles:Set [Role] = {
if(sub_role == null)sub_role = new java.util.HashSet [Role]
sub_role
}

@ManyToMany
private var permission:java.util。 Set [Permission] = _
def permissions:设置[Permission] = {
if(permission == null)permission = new java.util.HashSet [Permission]
permission
}
}

这是一个来自许多Scala / Hibernate项目之一的示例类我一直在努力。



您需要使用Java的一件事是自定义JSR-303验证注释。

As been asked before, it is not recommended to use Spring Data or Hibernate on Scala because it doesn't work well with it's byte code manipulation (and some interface incompatibilities).


My question is however, because it is possible to code Scala and Java in parallel with SBT, is it a good idea to code my repositories and services in Java and then use them in my Scala code? Does this get rid of the byte code constraint?

I'm asking here because I couldn't find any resources on this.

解决方案

There are no issues using Scala for Hibernate. You don't even need to use @BeanProperty. For example:

@Entity
class Role {
  @Id @GeneratedValue
  var id: Long = _

  @Index(name="role_name")
  var name: String = _

  var created_at: Date = _
  var updated_at: Date = _  

  @ManyToMany
  private var sub_role: java.util.Set[Role] = _
  def subRoles: Set[Role] = {
    if (sub_role == null) sub_role = new java.util.HashSet[Role]
    sub_role
  }

  @ManyToMany
  private var permission: java.util.Set[Permission] = _
  def permissions: Set[Permission] = {
    if (permission == null) permission = new java.util.HashSet[Permission]
    permission
  }
}

This is an example class from one of a number of Scala/Hibernate projects I've worked on.

One thing that you do need to use Java for is custom JSR-303 validation annotations.

这篇关于Spring数据与Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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