Question

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();
    }
Was it helpful?

Solution

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
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top