1

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.. :)

Prasanth A R
  • 4,046
  • 9
  • 48
  • 76

1 Answers1

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

Community
  • 1
  • 1
senia
  • 37,745
  • 4
  • 88
  • 129
  • 1
    @milano: if you want to override a method of parent class you should use [override modifier](http://www.artima.com/pins1ed/composition-and-inheritance.html#10.8). – senia Jul 08 '13 at 05:49