2

Now a days, I am learning Hibernate using IntelliJ. So, I created a Maven project and created a Hibernate program. But then I have got an error like the show in this image.

Stacktrace with file not found

Then I searched for solutions in the Internet. But I can't find any correct solution.

chalimartines
  • 5,603
  • 2
  • 23
  • 33
TTDS
  • 114
  • 1
  • 10
  • 1
    Did you try these: http://stackoverflow.com/questions/24065817/resources-hibernate-cfg-xml-not-found, http://stackoverflow.com/questions/4934330/org-hibernate-hibernateexception-hibernate-cfg-xml-not-found, http://stackoverflow.com/questions/8196370/hibernate-cfg-xml-not-found, http://stackoverflow.com/questions/15258594/hibernate-exception-hibernate-cfg-xml-not-found ? – lealceldeiro Apr 11 '17 at 15:28
  • Can you show how looks like your project directory structure? Where do you have hibernate.cfg.xml file in it? – chalimartines Apr 11 '17 at 17:18

3 Answers3

5

If you are working on IntelliJ Idea then make a folder named "resources" under src\main\java. Open Module setting of your project, select "Modules" from left and in the "sources" tab select the newly created "resources" folder and mark it as "Resources".enter image description here

then this should work.

Configuration con = new Configuration().configure("hibernate.cfg.xml");
sam
  • 1,800
  • 1
  • 25
  • 47
2

Here we go... This might help you if there anyone learning Hibernate in IntelliJ at 2020 :)

  1. Create a JAVA project in IntelliJ 2)Type Hibernate in the search box to locate the necessary plugin in the list. If the checkbox next to Hibernate is not selected, select it to enable the plugin. Click OK.

  2. Add JDBC driver and test connection
    using the common code snippet

if it working fine then add the Hibernate Config file to your root of the src directory

Terminology

Entity class : Java class that map to a database table

This Entity class should map to the actual database table

ORM - object-relational mapping

so there should be some method mapping this class to a db table

JAVA Annotations-Modern and Preferred one for mapping

step 1: Map the class to the database table

@Entity    
@Table(name="db_name")

step 2: Map fields to database columns

@Id(signify primary key)
@Column(name="column_name")

like here..

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {

@Id
@Column(name = "id")
private int id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "email")
private String email;

public Student(){

} 


}

Develop Java code to perform database operations

Two key players in Hibernate:

 SessionFactory: 

this one read the hibernate config file and create session objects. Just because of the heavy object we only create it once in-app and use it over and over again.

 Session : 

It's like a wrapper in JDBC connection and used to save/retrieve objects from the database. This is a short-lived object and retrieved from SessionFactory.

 public static void main(String [] args){

       SessionFactory factory = new Configuration()
                                        .configure("hibernate.cfg.xml")
                                        .addAnnotatedClass(Student.class)
                                        .buildSessionFactory();

       Session session = factory.getCurrentSession();


 try{
    
   // use the session object to save/ retrieve java objects
   


  //create a test db object
   Student temoStudent = new Student("Tim","NIke","timnike@gmail.com");

   // start transaction
   session.beginTransaction();

   // save the student
   session.save(tempStudent);

   //commit the transaction
   session.getTransaction().commit();



  }finally {

    factory.close();

  }

 }
DalusC
  • 391
  • 1
  • 12
0

Put your hibernate.cfg.xml file in "resource" folder.

In main you need to configure it like

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
NaveeN
  • 121
  • 6