9

我以这种方式构建我的 HibernateUtil:

public class HibernateUtil {

        private static final SessionFactory sessionFactory;

        static {
            try { 
                // Create the SessionFactory from standard (hibernate.cfg.xml) config file.
                sessionFactory = new Configuration().configure().buildSessionFactory();

            } catch (Throwable ex) {
                // Log the exception. 
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
}

因此,当我尝试在 Eclipse(使用 Hibernate 工具)的 HQL 编辑器中执行 HQL 命令时,会出现以下错误: 在此处输入图像描述 为什么会发生这种情况?它不应该通过 ConfigureAnnotation 更改 AnnotationConfiguration 吗?

更新

<?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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password"><password></property>
        <property name="hibernate.connection.url">jdbc:mysql://<hostname>:3306/<schema></property>
        <property name="hibernate.connection.username">root</property>
        <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- SQL -->
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.show_sql">true</property>
        <!-- C3P0 -->
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">180</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>
        <!-- Classes -->
        <mapping class="com.suaparte.pojo.Area" />
    </session-factory>
</hibernate-configuration>

提前致谢。

4

10 回答 10

12

如果您有该错误,并且您使用的是 >=4.0 的休眠版本,则问题可能出在休眠控制台配置中。

尝试前往:

运行 -> 运行配置

并打开您创建的配置,在主选项卡上将类型从核心更改为注释。这是一个屏幕截图: 在此处输入图像描述

于 2013-01-12T14:09:03.370 回答
8

只需将 Configuration() 更改为 AnnotationConfiguration()

于 2014-09-13T22:34:44.373 回答
4

我已将我的代码从

Configuration cfg=new Configuration();
            cfg.configure("hibernate.cfg.xml");         
            SessionFactory factory=cfg.buildSessionFactory();

SessionFactory factory=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();

此外,我还没有在我的 POJO 类中添加“@Id”注释。添加“@Id”后,我完全解决了我的问题。

于 2015-05-16T07:19:18.083 回答
3

您可以使用 AnnotationConfiguration() 而不是 Configuration()

于 2015-05-03T16:00:51.343 回答
2

尝试像这样构建:

AnnotationConfiguration().configure().buildSessionFactory();

为此,您需要:

Hibernate annotations

问候。

你做。

于 2011-09-19T19:15:16.900 回答
2

尝试下载hibernate分发jars 3.6.4版本,使用jre1.6.0_07版本。通过这样做,您可以成功地创建新的配置对象,如下所示,而不是使用AnnotationConfiguration().

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
于 2015-12-02T23:45:08.710 回答
1

尝试将 Configuration().configure().buildSessionFactory() 更改为 AnnotationConfiguration().configure().buildSessionFactory() 并在类路径中包含 hibernate-annotations.jar

于 2017-08-21T12:56:31.983 回答
1

使用 Hibernate 最新版本 jar 的 5.x 或以上

于 2019-03-12T02:15:34.030 回答
0
AnnotationConfiguration configuration=new AnnotationConfiguration();

    configuration.configure("hibernate.cfg.xml");

    SessionFactory factory=configuration.buildSessionFactory();

    Session session=factory.openSession();
于 2017-02-01T12:02:29.030 回答
-1

创建一个返回SessionFactory对象的实用程序类:

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {
       try {
           sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        catch(Throwable t) {
            throw new ExceptionInInitializerError(t);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

并在你的主类中调用它,如下所示:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
于 2012-06-11T07:06:14.703 回答