Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7847655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:58:28+00:00 2026-06-02T17:58:28+00:00

I’m new to Scala, so its probable that this is an incredibly obvious mistake.

  • 0

I’m new to Scala, so its probable that this is an incredibly obvious mistake. However, I’m trying to cast a List[Object] into List[A], where A is a parameter in the class.

class AbstractHibernateDAO[A<:Serializable]{
  def findAll: List[A] = {
    val objList = currentSession.createQuery("from " + clazz.getName()).list()
    objList.map{_.asInstanceOf[A]}
  }
}

Eclipse is puking up:

type mismatch;  found   : ?0(in method findAll) => A where type ?0(in method findAll)  required: (some other)?0(in method findAll) => ? where type (some other)?0(in method findAll)    AbstractHibernateDAO.scala  /springHibernateNoXML/src/main/scala/my/package

I’ve also tried the long form

objList.map{obj => obj.asInstanceOf[A]}

and get the same result.

Can anyone give me a hand?

[EDIT – Additional information]

For those that requested, here’s the full listing:

package name.me


import java.io.Serializable
import java.util.List
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.context.annotation.Scope
import org.springframework.orm.hibernate3.HibernateTemplate
import org.springframework.stereotype.Repository
import com.google.common.base.Preconditions
import org.hibernate.SessionFactory
import org.hibernate.Session
import collection.JavaConversions._

@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
class AbstractHibernateDAO[A<:Serializable]{

  val clazz: Class[A] = null
  @Autowired val sessionFactory: SessionFactory = null

  def currentSession: Session = {
    sessionFactory getCurrentSession
  }

  def findOne(id: Long): A = {
    Preconditions checkArgument(id != null)
    currentSession.get(clazz, id).asInstanceOf[A]
  }

  def findAll: List[A] = {
    val objList = currentSession.createQuery("from " + clazz.getName()).list()
    objList.map(_.asInstanceOf[A])
    //This works
    //objList.asInstanceOf[List[A]]
  }

  def save(entity:A){
    Preconditions checkNotNull entity
    currentSession persist entity
  }

  def update(entity: A){
    Preconditions checkNotNull entity
    currentSession merge entity
  }

  def delete(entity: A){
    Preconditions checkNotNull entity
    currentSession delete entity
  }

  def deleteById(entityId: Long){
    Preconditions checkNotNull entityId
    val entity = findOne(entityId) 
    Preconditions checkNotNull entity
    delete(entity)
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:58:29+00:00Added an answer on June 2, 2026 at 5:58 pm

    A quick look at the Hibernate API docs shows that Query.list() returns a java.util.List, but you have a type annotation of List[A], which is by default (from the scala package object) a scala.collection.immutable.List.

    Which do you want to return?

    for Java List, change your type annotation and cast the whole list:

    class AbstractHibernateDAO[A<:Serializable]{
      def findAll: java.util.List[A] = {
        val objList = currentSession.createQuery("from " + clazz.getName()).list()
        objList.asInstanceOf[java.util.List[A]]
      }
    }
    

    for Scala List, use JavaConversions:

    class AbstractHibernateDAO[A<:Serializable]{
      def findAll: List[A] = {
        val objList = currentSession.createQuery("from " + clazz.getName()).list()
        import collection.JavaConversions._
        objList.toList.asInstanceOf[List[A]]
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.