Hibernate: AnnotationConfiguration instance is needed to use ... error

I create my HibernateUtil as follows:

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

Therefore, when I try to execute the HQL command in the HQL editor in Eclipse (using Hibernate Tools), the following error appears: enter image description here Why is this happening? This will not change the setting of AnnotationConfiguration with 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.

+8
eclipse hibernate hibernate-tools
source share
9 answers

If you have this error and you are using hibernate> = 4.0, the problem is probably in the configuration of the Hibernate console.

Try going to:

Run → Run Configurations

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

+12
source share

Just change Configuration () to AnnotationConfiguration ()

+6
source share

I changed my code with

 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 have not added @Id annotations to my POJO class. After adding "@Id" I completely solved my problem.

+3
source share

Try building as:

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

To do this, you will need the following:

 Hibernate annotations 

Sincerely.

Udo.

+2
source share

Try downloading version hibernate distribution jars 3.6.4., Use version jre1.6.0_07. By doing this, you can successfully create a new configuration object, as shown below, instead of using AnnotationConfiguration() .

 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
+2
source share

you can use the AnnotationConfiguration () function instead of Configuration ()

+1
source share
 AnnotationConfiguration configuration=new AnnotationConfiguration(); configuration.configure("hibernate.cfg.xml"); SessionFactory factory=configuration.buildSessionFactory(); Session session=factory.openSession(); 
0
source share

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

0
source share

Create a utility class that returns 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 name it in your main class as follows:

 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); 
-one
source share

All Articles