Hibernate:需要 AnnotationConfiguration 实例才能使用...错误

发布于 2024-12-05 13:40:53 字数 2637 浏览 6 评论 0原文

我这样构建 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 中的 HQL 编辑器中执行 HQL 命令(使用 Hibernate Tools)时,会出现以下错误: 在此处输入图像描述 为什么会出现这种情况?它不应该通过 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>

提前致谢。

I build my HibernateUtil this way :

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;
        }
}

So when I try to execute the HQL command in HQL Editor in Eclipse (with Hibernate Tools) gives the follow error:
enter image description here
Why this happening ? It shouln't change the AnnotationConfiguration by ConfigureAnnotation ?

UPDATE

<?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>

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

我恋#小黄人 2024-12-12 13:40:53

如果你有这个错误,
并且您使用的 hibernate 版本 >=4.0,问题可能出在 Hibernate 控制台配置中。

尝试转到:

运行->运行配置

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

If you have that error,
and you are using hibernate version >=4.0, the problem probably is in the Hibernate Console configuration.

Try to go to:

Run -> Run Configurations

and open the configuration that you have created, On the main tab change Type from Core to Annotations. Here a screenshot:
enter image description here

も让我眼熟你 2024-12-12 13:40:53

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

Just change Configuration() to AnnotationConfiguration()

凯凯我们等你回来 2024-12-12 13:40:53

我已将代码从 更改为

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”后我完全解决了我的问题。

I have changed my code from

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

To

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

Also I havent added "@Id" annotations in my POJO class. After adding "@Id" I resolved my problem completly.

内心旳酸楚 2024-12-12 13:40:53

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

you can use AnnotationConfiguration() instead of Configuration()

陌伤ぢ 2024-12-12 13:40:53

尝试构建如下:

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

为此,您需要这个:

Hibernate annotations

问候。

乌多.

Try to build like:

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

To do so, you need this:

Hibernate annotations

Regards.

Udo.

用心笑 2024-12-12 13:40:53

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

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Try to download the hibernate distribution jars 3.6.4 version., use jre1.6.0_07 version.
by doing this, you can successfully create new configuration object as below instead of using AnnotationConfiguration().

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
离鸿 2024-12-12 13:40:53

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

Try changing Configuration().configure().buildSessionFactory() into AnnotationConfiguration().configure().buildSessionFactory() and include hibernate-annotations.jar in the class path

套路撩心 2024-12-12 13:40:53

使用 Hibernate 最新版本 jar 的 5.x 或更高版本

Use Hibernate latest version jar's 5.x or above

最初的梦 2024-12-12 13:40:53
AnnotationConfiguration configuration=new AnnotationConfiguration();

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

    SessionFactory factory=configuration.buildSessionFactory();

    Session session=factory.openSession();
AnnotationConfiguration configuration=new AnnotationConfiguration();

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

    SessionFactory factory=configuration.buildSessionFactory();

    Session session=factory.openSession();
记忆之渊 2024-12-12 13:40:53

创建一个返回 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();

Create a utility class which return a SessionFactory object:

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();
    }
}

and call this in your main class like this:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文