0
votes

I have an existing Java application which I am trying to migrate to scala. I have an abstract class defined like this

public abstract class AbstractHibernateDao<T, ID extends Serializable> extends
    HibernateDaoSupport implements GenericDao<T, ID> {

     private final Class<? extends T> persistentClass;

     // spring parts-----

     public AbstractHibernateDao(Class<? extends T> persistentClass) {
           this.persistentClass = persistentClass;
     }

the constructor accepts a parameter of type Class. I then write a scala class which extends AbstractHibernateDao.

package com.worldcorpservices.core.dao.shares.hibernate.scala {

import com.worldcorpservices.core.dao.AbstractHibernateDao
import com.worldcorpservices.core.dao.shares.VolatilityDao
import com.worldcorpservices.core.domain.shares.scala.Volatility
import com.worldcorpservices.core.domain.shares.scala.VolatilityId
import com.worldcorpservices.core.domain.shares.scala.VolatilityImpl
import java.util.List
import org.hibernate.criterion.DetachedCriteria
import org.hibernate.criterion.Order
import org.hibernate.criterion.Restrictions

class ScalaHibernateVolatilityDaoImpl(persistentClass : classTag[VolatilityImpl]) 
    extends AbstractHibernateDao[Volatility, VolatilityId](persistentClass)
    with VolatilityDao {

          ......

I build my app using maven, and here's my pom.xml

<dependencies>
    <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-compiler</artifactId>
                <version>${scala.version}</version>
    </dependency>
    <dependency>
               <groupId>org.scala-lang</groupId>
               <artifactId>scala-library</artifactId>
               <version>${scala.version}</version>
        </dependency>
...

Somehow, when I run mvn scala:compile I am getting this error

[ERROR]  error: not found: type classTag

I have tried to use classOf,but got exactly same error (not found:type classOf)

2

2 Answers

1
votes

You're trying to input a class object (the object that represents a class) as a type identifier (the identifier of a class, consisting of its name and generic constraints in case of generic classes) . Just use VolatilityImpl directly, i.e. :

class ScalaHibernateVolatilityDaoImpl(persistentClass : VolatilityImpl)

This is unrelated to Maven, by the way.

0
votes

If I understand correctly, you want to extend generic DAO to get a DAO for your class. Then you have to write something like

class ScalaHibernateVolatilityDaoImpl
    extends AbstractHibernateDao[Volatility, VolatilityId](classOf[VolatilityImpl])
    with VolatilityDao

Note that you don't need any constructor parameters, hence you don't need Class type. But just FYI, Java Class and Scala Class are in direct correspondence, so if you need a variable of type Class<?> (or Class<? extends T>) but in Scala you just write Class[_] (or Class[_ <: T]).

classOf[T] function allows you to get Java class for the type argument. ClassTag is related, but different concept, which you don't need unless you use an API which is ClassTag-aware, but this is not your case since your API is in Java.