Hibernate TABLE未映射

时间:2011-08-18 17:12:25

标签: java hibernate jboss

我收到此错误

11:55:43,125 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] (MSC service thread 1-16) Not binding factory to JNDI, no JNDI name configured
11:55:43,215 ERROR [stderr] (MSC service thread 1-16) org.hibernate.hql.ast.QuerySyntaxException: ServerSettings is not mapped [from ServerSettings as ss]
11:55:43,215 ERROR [stderr] (MSC service thread 1-16)   at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
11:55:43,215 ERROR [stderr] (MSC service thread 1-16)   at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
11:55:43,216 ERROR [stderr] (MSC service thread 1-16)   at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)

尝试部署我的应用程序时。

我的类名为ServerSettings.java - 它看起来像

static public ServerSettings GetServerSettings() throws Exception
{
    List retList = null;
    ServerSettings ss = null;
    try {
        Session hsession = HibernateUtil.currentSession();
        retList = hsession.createQuery("from ServerSettings as ss").list();
        if (retList == null || retList.size() <= 0)
            return null;
        ss = (ServerSettings) retList.get(0);
    } 
    finally
    {
        HibernateUtil.closeSession();
    }
    return ss;

我们的persistence.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="primary">

  <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
  <properties>
     <!-- Properties for Hibernate -->
     <!-- <sproperty name="hibernate.hbm2ddl.auto" value="create-drop" /> -->
     <!-- <property name="hibernate.show_sql" value="false" /> -->
  </properties>

我们还有一个hibernate.cfg.xml

    

   <session-factory>
      <property name="datasourceName">java:jboss/datasources/MySqlDS</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

   </session-factory>

</hibernate-configuration>

和ServerSettings.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
        <class name="com.mycompanyServerSettings" table="server_settings">
        <id name="serverSettingsID" type="integer" column="server_settings_id"> 
            <generator class="identity" />
        </id>
        <version  name="updateCounter" column="update_counter"/>
        <property name="changedDate" type="timestamp" column="changed_date"/>
        <property name="changedBy" type="string" column="changed_by"/>
        <property name="createdDate" type="timestamp" column="created_date"/>
        <property name="createdBy" type="string" column="created_by"/>
        <property name="status" type="string" column="status"/>

        <property name="defaultJmsQueueName" type="string" column="default_jms_queue_name"   />
        <property name="defaultJmsQueueURL" type="string" column="default_jms_queue_url"   />
        <property name="emailServer" type="string" column="email_server"   />
        <property name="emailFromAddress" type="string" column="email_from_address"   />
        <property name="emailUser" type="string" column="email_user"   />
        <property name="emailPassword" type="string" column="email_password"   />
        <property name="defaultJMSQueueID" type="string" column="default_jms_queue_id"   />
        <property name="useEncryption" type="integer" column="use_encryption"   />

</class>

通常在hql中使用表名而不是类名时会导致此错误 - 但这不是这种情况。这也是以前在JBoss AS5中运行的 - 我正在将它转移到JBoss AS7。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

<class name="com.mycompanyServerSettings"
                          ^-- A dot is missing here

此外,主ServerSettings.hbm.xml文件中未引用hibernate.cfg.xml。你应该在session-factory元素中有这样一个元素:

<mapping resource="com/mycompany/ServerSettings.hbm.xml"/>