简体   繁体   中英

Java program convert to scala the interoperability problems

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();
    }

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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM