سؤال

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