【问题标题】:Hibernate: the session factory does not read the entity mapping from hibernate.cfg.xmlHibernate:会话工厂不从 hibernate.cfg.xml 读取实体映射
【发布时间】: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


    【解决方案1】:

    在使用方法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)

    【讨论】:

    • 如果我添加以下内容,程序运行良好:"sessionFactory = configuration.addAnnotatedClass(Tbl1.class).addAnnotatedClass(Tbl2.class).addAnnotatedClass(Tbl3.class).addAnnotatedClass(Tbl4.class)。 buildSessionFactory(serviceRegistry);"但我希望会话工厂从 hibernate.cfg.xml 中读取映射类。我认为 paser 不读取 hibernate.cfg.xml 中的标签映射
    • 代替hibernate.connection.username试试connection.username
    • 在您的hibernate.cfg.xml 文件中也没有结束标记&lt;/hibernate-configuration&gt;
    • 你是否遇到同样的异常?
    • 能否请您发布您的异常堆栈跟踪。
    【解决方案2】:

    如果通过配置文件(xml 文件)加载,您可能会丢失 .hbm 文件。尝试为每个表添加 hbm 文件。

    【讨论】:

    • 如果我使用注释(@Entity、@Column 等),文件 hbm 也是必需的?
    • 不,只有在xml文件配置的情况下才需要。在注解的情况下,java中的类和db中的表使用@Entity注解映射。
    【解决方案3】:

    我认为您缺少 hibernate.cfg.xml 文件。我的意思是如果您的cfg文件名与hibernate.cfg.xml不同,则需要配置Configuration configuration = new Configuration(); configuration.configure("filename.cfg.xml");。因为默认情况下休眠需要hibernate.cfg.xml文件而不在configuration.configure();中配置但如果cfg文件名不同则需要放置它在配置()中。您的文件需要放在src 文件夹中。如果你把它放在不同的地方,那么需要用正确的路径传递它。

    【讨论】:

    • 不,找到文件hibernate.cfg.xml,因为连接的属性被读取,但是它被忽略了标签
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2016-10-02
    • 1970-01-01
    • 2012-08-30
    • 2018-12-10
    • 2014-09-15
    相关资源
    最近更新 更多