使用Play2 + Hibernate的ClassNotFoundException

时间:2016-01-27 09:02:14

标签: mysql hibernate scala playframework-2.0 hibernate-mapping

我是基于JVM的新手,我正在尝试使用Scala,Play 2框架和Hibernate构建测试Web服务。我的问题是Hibernate配置。我的hibernate.cfg.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/parking-webservice
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            root
        </property>

        <mapping resource="hibernate.hbm.xml" package="models"/>

    </session-factory>
</hibernate-configuration>

hibernate.hbm.xml看起来像这样:

 <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="models">

        <class name="Server" table="load_balancer_server" schema="parking-webservice">
            <id name="id" column="id"/>
            <property name="url" column="url"/>
        </class>
        <class name="PlateRecognitionLogEntry" table="log_platerecognitionlogentry" schema="parking-webservice">
            <id name="id" column="id"/>
            <property name="plateNumber" column="plate_number"/>
            <property name="confidence" column="confidence"/>
            <property name="countryType" column="country_type"/>
            <property name="time" column="time"/>
            <property name="image" column="image"/>
            <property name="ipAddress" column="ip_address"/>
        </class>
        <class name="User" table="parking_auth_user" schema="parking-webservice">
            <id name="id" column="id"/>
            <property name="token" column="token"/>
            <property name="admin" column="admin"/>
            <property name="username" column="username"/>
        </class>
    </hibernate-mapping>

IntelliJ IDEA为我制作了一个Play 2项目。它在controllers目录中有viewsapp个目录。我也在那里创建了models包并在那里添加了我的类。其中一个类看起来像这样(生成的Java代码转换为Scala):

package models
class Server {
  private var id: Int = 0
  private var url: String = null

  def getId: Int = id

  def setId(id: Int) {
    this.id = id
  }

  def getUrl: String = url

  def setUrl(url: String) {
    this.url = url
  }

  override def hashCode: Int = {
    var result: Int = id
    result = 31 * result + (if (url != null) url.hashCode else 0)
    result
  }
}

我想在我的控制器中做什么:

object Application extends Controller {
  private val _configuration = (new Configuration()).configure()
  private val _serviceRegistry = new ServiceRegistryBuilder().applySettings(_configuration.getProperties).buildServiceRegistry()
  private val _sessionFactory = _configuration.buildSessionFactory(_serviceRegistry)

  def sessionFactory = _sessionFactory

  def server_get = Action { request =>
    val session = sessionFactory.openSession
    val query = session.createQuery("from Session")
    val servers: Seq[Server] = Scala.asScala(query.list).map({case d: Server => d})
    val responseList = servers.map(s => s.getUrl)
    val result = Json.newObject
    result.set("servers", Json.toJson(responseList))
    Ok(result.toString)
  }
}

我得到了这个例外:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ExceptionInInitializerError: null]]
...<unuseful stack trace>...
Caused by: java.lang.ExceptionInInitializerError: null
...<more stack trace>...
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate.hbm.xml
...<stack trace that refers to my (new Configuration()).configure() call>...
Caused by: org.hibernate.MappingException: class models.Server not found while looking for property: id
...<more stack trace>...
Caused by: java.lang.ClassNotFoundException: models.Server
...<stack trace that says URLClassLoader.findClass failed>...

我尝试从package删除.hbm.cfg属性并添加完全限定的类名,例如models.Server,但没有成功,会收到同样的错误。我要么不知道Play 2课程的完全限定名称是什么,要么我做错了一些不同的事情。

感谢任何帮助

0 个答案:

没有答案