如何在Scala中实现org.hibernate.Session

时间:2014-03-06 17:42:49

标签: hibernate scala

“Session”接口有两种方法,在Scala中由于类型擦除而减少为具有相同的签名:

public ProcedureCall createStoredProcedureCall(String procedureName, Class... resultClasses)

public ProcedureCall createStoredProcedureCall(String procedureName, String... resultSetMappings)

尝试实现它们会产生错误:

错误:双重定义: 方法createStoredProcedureCall:(procedureName:String,resultSetMappings:String *)org.hibernate.procedure.ProcedureCall and 方法createStoredProcedureCall:(procedureName:String,resultClasses:Class [_] )org.hibernate.procedure.ProcedureCall at 199 擦除后具有相同的类型:(procedureName:String,resultSetMappings:Seq)org.hibernate.procedure.ProcedureCall     override def createStoredProcedureCall(procedureName:String,resultSetMappings:String ):ProcedureCall = null                  ^

那么我将如何以编译和工作的方式实现此接口呢?

1 个答案:

答案 0 :(得分:1)

正如serejja指出的那样,你可以用Java写一个垫片来为你做翻译:

爪哇:

package jibernate;

import org.hibernate.procedure.ProcedureCall;
import org.hibernate.SharedSessionContract;

public abstract class Yava implements SharedSessionContract {

  public ProcedureCall createStoredProcedureCallC(String n, Class... xs) {
    return createStoredProcedureCall(n, xs);
  }

  public ProcedureCall createStoredProcedureCallS(String n, String... xs) {
    return createStoredProcedureCall(n, xs);
  }

}

如果你正在使用sbt,它可以存在于 src / main / java / jibernate / Yava.java

Scala的:

package whybernate

trait Mehssion extends jibernate.Yava {

  import org.hibernate.procedure.ProcedureCall

  def createStoredProcedureCall(n: String, h: Class[_], t: Class[_]*) =
    createStoredProcedureCallC(n, (h +: t):_*)

  def createStoredProcedureCall(n: String, h: String, t: String*) =
    createStoredProcedureCallS(n, (h +: t):_*)

}

你可以用同样的方式调用这些函数,利用你保证至少有一个所需的varargs的事实。