私はScalaを初めて使うので、これは非常に明白な間違いである可能性があります。しかし、List [Object]をList [A]にキャストしようとしています。ここで、Aはクラスのパラメータです。スカラマップメソッドが期待通りに動作しない
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
は、私はまた、長い形式に
objList.map{obj => obj.asInstanceOf[A]}
を試みたと同じ結果を得る:
class AbstractHibernateDAO[A<:Serializable]{
def findAll: List[A] = {
val objList = currentSession.createQuery("from " + clazz.getName()).list()
objList.map{_.asInstanceOf[A]}
}
}
Eclipseがアップ吐いています。
誰も私に手を差し伸べることはできますか?
- 要求されたものについては、[EDIT追加情報]
、ここでの完全なリストがあります:あなたはobjList
の種類の確認
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)
}
}
? –