这是我的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>
这就是hibernate util文件:
当我尝试执行一个简单的查询时,我得到了例外 - &gt; “查询异常:表未映射”。 但是,如果我按如下方式更改hibernate util,
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]
你可以帮助我吗?
答案 0 :(得分:0)
在使用方法addAnnotatedClass
时,您编辑了Tbl1, Tbl2, Tbl3 (2 times)
,正如刚刚编辑和删除的代码中所提到的那样。 .addAnnotatedClass(Tbl1.class).addAnnotatedClass(Tbl2.class).addAnnotatedClass(Tbl3.class).addAnnotatedClass(Tbl3.class)
。
使用xml
配置时,您说您有四个带注释的类。如上所述
<mapping class= "it.test.Tbl1"></mapping>
<mapping class= "it.test.Tbl2"></mapping>
<mapping class= "it.test.Tbl3"></mapping>
<mapping class= "it.test.Tbl4"></mapping>
请检查Tbl4.class
的映射是否存在,或尝试将上次addAnnotatedClass(Tbl3.class)
修改为.addAnnotatedClass(Tbl4.class)
。
答案 1 :(得分:0)
在通过配置文件(xml文件)加载的情况下,可能缺少.hbm文件。尝试为每个表添加hbm文件。
答案 2 :(得分:0)
我认为您遗失了hibernate.cfg.xml
个文件。我的意思是如果你的cfg文件名不同于hibernate.cfg.xml
,那么需要配置Configuration configuration = new Configuration(); configuration.configure("filename.cfg.xml");
。因为默认情况下hibernate接受hibernate.cfg.xml文件而不在configuration.configure();
中配置但是如果cfg文件名是不同的,然后需要把它放在configure()中。您的文件需要放在src
文件夹中。如果你把它放在不同的地方,那么需要用正确的路径传递它。