2

I am creating a simple application to learn Hibernate. I am using NetBeans IDE and I created a class in com.hibernate package. The class is defined as:

package com.hibernate;

import com.mahesh.entity.UserDetails; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class hibr {
    public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserID(1);
        user.setUserName("Mahesh");

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }
}

I have defined UserDetails class as:

package com.mahesh.entity;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 *
 * @author Mahesh
 */
@Entity
public class UserDetails {
    @Id
    private int userID;
    private String userName;

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserID() {
        return userID;
    }

    public String getUserName() {
        return userName;
    }
}

I have defined a hibernate.cfg.xml file which is in src folder(default package)

This is the error generated by NetBeans IDE.

Feb 27, 2012 8:51:35 AM org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml Feb 27, 2012 8:51:35 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: /hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405) at org.hibernate.cfg.Configuration.configure(Configuration.java:1427) at org.hibernate.cfg.Configuration.configure(Configuration.java:1414) at com.hibernate.hibr.main(hibr.java:18) Java Result: 1

3 Answers 3

2

try

new Configuration().configure(<your cfg file path>).buildSessionFactory();
Sign up to request clarification or add additional context in comments.

2 Comments

Please tell me path to write. i have my cfg file in src folder of NetBeans project
if you kept it in src folder then no need to give path, make sure that file from src copied in build folder of netbeans
1

Make sure hibernate.cfg.xml also in your classpath in order that JVM can see it.

2 Comments

how to add hibernate.cfg.xml file in classpath i am working in NetBeans.
i try to add file path in system classpath but its working. Please help me
1
new Configuration().configure()

The configure() is referring to the classpath in build folder. I used to copy it from the src folder to build folder manually. After clean, the build folder will be removed. So, I have to repeat it. Is there any way to automate this process?

I have considered

new Configuration().configure(new File("a path/hibernate.cfg.xml"))

But the hbm file descriptions in hibernate.cfg.xml also involve a resource path. How can I mirror these files in build directory exactly as the source directory automatically?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.