Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 790
Valter Silva Avatar asked Sep 19 '11 12:09

Valter Silva


4 Answers

you can use AnnotationConfiguration() instead of Configuration()

like image 79
Vicks Avatar answered Oct 06 '22 22:10

Vicks


Just change Configuration() to AnnotationConfiguration()

like image 34
Karan Avatar answered Nov 18 '22 16:11

Karan


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

like image 12
Ivan Avatar answered Nov 18 '22 16:11

Ivan


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.

like image 4
Bhagwat K Avatar answered Nov 18 '22 17:11

Bhagwat K