java 休眠表未映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7111379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate TABLE is not mapped
提问by user619804
I receive this error
我收到此错误
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)
When attempting to deploy my application.
尝试部署我的应用程序时。
My class is called ServerSettings.java - it looks like
我的课程叫做 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;
our persistence.xml looks like
我们的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>
We also have a hibernate.cfg.xml
我们还有一个 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>
and a ServerSettings.hbm.xml
和一个 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>
Usually this error is caused when using table name instead of class name in hql - but that's not the case here. Also this was working previously in JBoss AS5 - I'm moving it to JBoss AS7. Any advice?
通常这个错误是在 hql 中使用表名而不是类名时引起的 - 但这里不是这种情况。这也是以前在 JBoss AS5 中工作的 - 我将它移到 JBoss AS7。有什么建议吗?
回答by JB Nizet
<class name="com.mycompanyServerSettings"
^-- A dot is missing here
Moreover, the ServerSettings.hbm.xml
isn't referenced in the main hibernate.cfg.xml
file. You should have such an element in the session-factory
element:
此外,ServerSettings.hbm.xml
主hibernate.cfg.xml
文件中未引用。你应该在元素中有这样一个session-factory
元素:
<mapping resource="com/mycompany/ServerSettings.hbm.xml"/>