4

Here java list convertion errors are occured

Scala Code

 @SuppressWarnings("unchecked") 
  @Override
  def getAllStudents():List[Student] = {
    return getSession().createQuery("from Student where isDelete =  'false' ")
    .list()  **here error occured and that shows below **
     }

here i import this statement but no change

import scala.collection.JavaConverters._

Error type mismatch; found : java.util.List[?0] where type ?0 required: scala.collection.immutable.List[com.model.domain.entity.Student]

The Java Code

@SuppressWarnings("unchecked")
    @Override
    public List<Student> getAllStudents() {
        return getSession().createQuery(
                "from Student where isDelete =  'false' ").list();
    }
2
  • Where you have placed import scala.collection.JavaConverters._? Commented Jul 4, 2013 at 7:07
  • @om-nom-nom top of program... Commented Jul 4, 2013 at 7:19

1 Answer 1

6

Your own answer is incorrect. Instead, if you are implementing an interface (or extending a class) which needs to return a Java list, you should do this:

def getAllStudents() : java.util.List[Student] = {
  getSession().createQuery("from Student where isDelete =  'false' ")
    .list()
 }

If you don't need Java list here, then you should instead do

import scala.collection.JavaConverters._

def getAllStudents() : Seq[Student] = {
  getSession().createQuery("from Student where isDelete =  'false' ")
    .list().asScala
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Worth mentioning that if you use JavaConverters to convert let's say a java.util.List to a Seq and later back to a java.util.List, it remembers the original list and yields it at the later conversion. See Time complexity of JavaConverters asScala method.

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.