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

4
  • 1
    You want <- instead of : in the for in Scala, and you don't need the var entity line at all. Commented Jul 8, 2013 at 5:31
  • @RexKerr u r r8.. pls answer it and i will mark as correct answer and this is really help full ans thankx... Commented Jul 8, 2013 at 5:35
  • 1
    senia's got the more complete correct answer. Commented Jul 8, 2013 at 5:41
  • @RexKerr mm.. okay.. your r r8.. and really thanks for your comments.. it's really helpful Commented Jul 8, 2013 at 5:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@milano: if you want to override a method of parent class you should use override modifier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.