Sunday, 18 January 2015

SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration

You might have encountered this exception while mapping your POJO classes to tables in hibernate.


Exception:
SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.javacodegeeks.enterprise.hibernate.Student"/>
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.javacodegeeks.enterprise.hibernate.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at com.javacodegeeks.enterprise.hibernate.utils.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.javacodegeeks.enterprise.hibernate.Test.main(Test.java:14)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.javacodegeeks.enterprise.hibernate.Student"/>
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1524)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
    at com.javacodegeeks.enterprise.hibernate.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
   
   
   
Solution:


If you see above exception it means you are not loading configuration from AnnotationConfiguration please load your configuration as below:

return new AnnotationConfiguration().configure().buildSessionFactory();

For loading configuration from XML file we use below code:

return new Configuration().configure().buildSessionFactory();

No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...