문제

i am working with scala in few days. and i am really confused with this problem .

i really tried to solve the peoblem but i can't..

My java code

@Override
    public void saveOrUpdateAll(Collection<T> entities) {
        Session session = getSession();
        for (T entity : entities) {
            session.saveOrUpdate(entity);
        }
    }

Scala Code

@Override
    def  saveOrUpdateAll( entities:Collection[T]){
        var session:Session = getSession()
        var entity:T=null
        for (entity :entities) {
            session.saveOrUpdate(entity);
        }
    }

search for scala for each. and am really confused about that .. if you know how to solve this problem please share your answer here.. and Thanx..

with regards Milano.. :)

도움이 되었습니까?

해결책

override def saveOrUpdateAll(entities: Collection[T]){
  import scala.collection.JavaConverters._

  val session: Session = getSession()

  for (entity <- entities.asScala) {
      session.saveOrUpdate(entity)
  }
}

There is no for each loop in scala. You should wrap your collection using JavaConverters and use for-comprehension here.

JavaConverters wraps Collection using Wrappers.JCollectionWrapper without memory overhead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top