【发布时间】:2016-12-13 09:02:25
【问题描述】:
这是我的 hibernate.cfg:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Connessione al database -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:xe
</property>
<!-- Credenziali -->
<property name="hibernate.connection.username">Test</property>
<property name="connection.password">Test</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.Oracle10gDialect</property>
<!-- DISABILITA AUTO COMMIT -->
<property name="hibernate.connection.autocommit">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Entity -->
<mapping class= "it.test.Tbl1"></mapping>
<mapping class= "it.test.Tbl2"></mapping>
<mapping class= "it.test.Tbl3"></mapping>
<mapping class= "it.test.Tbl4"></mapping>
</session-factory>
</hibernate-configuration>
这是休眠实用程序文件:
当我尝试执行一个简单的查询时,我得到了异常 -> “查询异常:表未映射”。 但是,如果我按如下方式更改休眠实用程序,
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null)
sessionFactory = createSessionFactory();
return sessionFactory;
}
}
该程序运行成功。 为什么没有通过配置文件加载会话工厂?
控制台日志:
10:54:44.989 [main] DEBUG org.hibernate.hql.internal.ast.ErrorCounte-
throwQueryException() : no errors
10:54:45.130 [main] DEBUG
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker - select << begin
[level=1, statement=select]
org.hibernate.hql.internal.ast.QuerySyntaxException: Tbl1 is not mapped
[from Tbl1 eat where eat.activityId = :id]
at
org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException
(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString
(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile
(QueryTranslatorImpl.java:218)
我认为问题是这样的:
17:14:47.130 [main] DEBUG org.hibernate.internal.SessionFactoryRegistry -
Registering SessionFactory: a0044811-5a9f-483a-8ede-b136c9781bb3
(<unnamed>)
17:14:47.130 [main] DEBUG org.hibernate.internal.SessionFactoryRegistry -
Not binding SessionFactory to JNDI, no JNDI name configured
17:14:47.364 [main] DEBUG org.hibernate.stat.internal.StatisticsInitiator
- Statistics initialized [enabled=false]
你能帮帮我吗?
【问题讨论】:
标签: java xml hibernate maven jndi