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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7470619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:32:30  来源:igfitidea点击:

Hibernate: An AnnotationConfiguration instance is required to use ... error

eclipsehibernatehibernate-tools

提问by Valter Silva

I build my HibernateUtil this way :

我以这种方式构建我的 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;
        }
}

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

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

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.

提前致谢。

回答by Ivan

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

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

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

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

回答by Karan

Just change Configuration() to AnnotationConfiguration()

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

回答by Ragnar

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.

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

回答by Vicks

you can use AnnotationConfiguration() instead of Configuration()

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

回答by ssedano

Try to build like:

尝试构建如下:

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

To do so, you need this:

为此,您需要:

Hibernate annotations

Regards.

问候。

Udo.

你做。

回答by Durga P N

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().

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

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

回答by Arunprasad

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

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

回答by Ramkrishna Sitap

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

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

回答by user7500255

AnnotationConfiguration configuration=new AnnotationConfiguration();

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

    SessionFactory factory=configuration.buildSessionFactory();

    Session session=factory.openSession();

回答by sunil_patidar

Create a utility class which return a SessionFactoryobject:

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

and call this in your main class like this:

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

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();