Hibernate: An AnnotationConfiguration instance is required to use ... error
Asked Answered
M

10

9

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.

Mascot answered 19/9, 2011 at 12:2 Comment(1)
Can you paste the hibernate.cfg.xml? You can omit the classes if you like thou. The properties.Stunk
D
12

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

Decoder answered 12/1, 2013 at 14:9 Comment(0)
I
8

Just change Configuration() to AnnotationConfiguration()

Iota answered 19/9, 2011 at 12:2 Comment(1)
Using AnnotationConfiguration is deprecated.Spongioblast
P
4

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.

Pyrochemical answered 16/5, 2015 at 7:19 Comment(0)
E
3

you can use AnnotationConfiguration() instead of Configuration()

Edea answered 3/5, 2015 at 16:0 Comment(0)
S
2

Try to build like:

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

To do so, you need this:

Hibernate annotations

Regards.

Udo.

Stunk answered 19/9, 2011 at 19:15 Comment(0)
A
2

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();
Aldred answered 2/12, 2015 at 23:45 Comment(0)
P
1

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

Paroicous answered 21/8, 2017 at 12:56 Comment(0)
D
1

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

Dicarlo answered 12/3, 2019 at 2:15 Comment(0)
L
0
AnnotationConfiguration configuration=new AnnotationConfiguration();

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

    SessionFactory factory=configuration.buildSessionFactory();

    Session session=factory.openSession();
Lorraine answered 1/2, 2017 at 12:2 Comment(1)
Welcome to stack overflow :-) Code-only answers aren’t useful for the community. Please look at How to AnswerProtestation
L
-1

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();
Leucoplast answered 11/6, 2012 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.