hibernate.cfg.xml file not found - should I make it and put it in the project directory?

1.5k views Asked by At

This is my first time using hibernate, I have gone through a tutorial or two and I have quickly mocked this class up to see if I can get something from my database:

import java.util.Properties;

import org.eclipse.emf.teneo.PersistenceOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;



public class Connector {

    public static void doStuff() {

        Properties props = new Properties();
        props.setProperty(Environment.DRIVER, "com.postgres.Driver");
        props.setProperty(Environment.USER, "postgres");
        props.setProperty(Environment.URL, "jdbc:postgres://127.0.0.1:5432/epic");
        props.setProperty(Environment.PASS, "postgres");
        props.setProperty(Environment.DIALECT, org.hibernate.dialect.ProgressDialect.class.getName());

        props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
                "REFRESH,PERSIST,MERGE");

        props.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");// populates the data of the
                                            // configuration file

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Transaction t = session.beginTransaction();

        Query query = session.createQuery("select * from otar");
        java.util.List list = query.list();
        System.out.println(list);
        t.commit();
        session.close();
    }

    public static void main(String[] args) {
        doStuff();
    }

}

the compiler says that I am missing the hibernate.cfg.xml file, I have read that it needs to be in my src directory of my project, I have also read that it doesn't get generated for me, but I have also read the opposite that it does get generated for me ... my understanding of this file is that it is needed for table column mapping...it probably has a much more complex and useful use but I have only just started this so that is as much as I know, here are my questions:

  1. Does it get generated for me or do I need to create it?
  2. Is it ok to create? like is it good practice?
  3. what are the benefits of me making it by hand.
  4. Are there any instructions on how to go about creating one?

EDIT I forgot to mention that this is a plug-in project that I am using.

3

There are 3 answers

0
Roman C On BEST ANSWER

There's a difference between Hibernate configuration file (hibernate.cfg.xml) and Hibernate mapping files (*.hbm). If you have heard about generated code it concerns last ones, but first one should have included those mapping files. There's two or three different approaches to development. First to create Java classes with mappings and generate database schema, Second create database schema and generate Java classes (reverse engineering), Third is create classes and mapping for existed schema. Whichever approach you use is up to you, but hibernate.cfg.xml you should create manually if it's not created by IDE. Some frameworks like Spring can provide it's own configuration for Hibernate, thus completely ignoring hibernate.cfg.xml. There's nothing magical with creating hibernate.cfg.xml file. You can start with

<?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>
  <!-- here you can place properties and mapping -->

  </session-factory>

</hibernate-configuration>

And you are right, it should be in the src folder of your project. At runtime it should be where the .class files generated, i.e. on classpath.

0
Xavier Bouclet On

Or you could use annotations and Spring so you won

Example :

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(
            dataSource);

    sessionBuilder.addAnnotatedClasses(Pojo1.class);
    sessionBuilder.addAnnotatedClasses(Pojo2.class);

    sessionBuilder.addProperties(getHibernateProperties());
    return sessionBuilder.buildSessionFactory();
}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.dialect",
            "org.hibernate.dialect.PostgreSQLDialect");
    return properties;
}
0
Bahram Sheibani On

hibernate.cfg.xml should be in classpath. please add it to classpath or in classes folder.