Consider the hierarchy :
And the following classes and xml :
Add the following code to hibernate.cfg.xml
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
Instead of using HibernateUtil.java, to create sessionfactory object, you should use this:
SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
Because in order to avoid the exception, you'll have to declare the class object in HibernateUtil.java file as configuration.addAnnotatedClass(Student_Info.class);
which looks dumb because we have provided the entry already in hibernate.cfg.xml file.
To use the AnnotationConfiguration class you'll have to add a jar to your project build path: http://www.java2s.com/Code/Jar/h/Downloadhibernate353jar.htm
I was having same problem.
Use @javax.persistence.Entity
instead of org.hibernate.annotations.Entity
I was having similar issue and adding
sessionFactory.setAnnotatedClasses(User.class);
this line helped but before that I was having
sessionFactory.setPackagesToScan(new String[] { "com.rg.spring.model" });
I am not sure why that one is not working.User class is under com.rg.spring.model Please let me know how to get it working via packagesToScan method.
The Hibernate configuration file must define the entity classes:
<mapping class="annotations.Users"/>
Or you must explicitly add the class to the configuration using
configuration.addClass(annotations.Users.class)
// Read mappings as a application resourceName
// addResource is for add hbml.xml files in case of declarative approach
configuration.addResource("myFile.hbm.xml"); // not hibernateAnnotations.cfg.xml
In my case it resolves by adding configuration.addAnnotatedClass(com.myApp.model.Example.class);
after Configuration configuration = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml"));
in hibernateUtill class.
It Read a mapping from the class annotation metadata .
Read more about addAnnotatedClass()
from here.