Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it mandatory to have hibernate.cfg.xml file for configuration

I do not want to have the hibernate.cfg.xml file. Rather I want to do all the configuration through my code as shown below.

private static final SessionFactory factory;
private static final Properties properties;

static
{
    properties = new Properties();
    properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/books");
    properties.setProperty("hibernate.connection.username", "jhtp7");
    properties.setProperty("hibernate.connection.password", "password");
    properties.setProperty("hibernate.show_sql", "com.mysql.jdbc.Driver");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

    factory = new Configuration().setProperties(properties).configure().
                            buildSessionFactory();
}

I have tried the above approach. But I am facing the problem where hibernate throws an exception saying "./hibernate.cfg.xml file missing".

Is it really mandatory to keep the hibernate.cfg.xml file?

Thanks in advance

like image 214
Keen Sage Avatar asked Jan 12 '12 05:01

Keen Sage


3 Answers

I believe it is because of your configure() call on the Configuration object. Try removing that and hibernate will not look for the non-existent file. Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate.cfg.xml file which is exactly what the configure() method does.

like image 67
Swaranga Sarma Avatar answered Oct 12 '22 02:10

Swaranga Sarma


No, I don't think the config xml is mandatory. To fix your problem, I think you need to use the org.hibernate.cfg.Configuration class. Have a look at this link: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

Basically, you need to things like

Configuration cfg = new Configuration() .setProperty("hibernate.dialect", "com.mysql.jdbc.Driver") .setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost:3306/books") .setProperty("hibernate.order_updates", "true"); 

Then to create your sessionFactory, you just say, cfg.buildSessionFactory();

like image 26
HeavenAgain Avatar answered Oct 12 '22 01:10

HeavenAgain


No, it's not mandatory to use hibernate.cfg.xml. Just don't use .configure(). Hibernate will look for hibernate.cfg.xml if we use .configure().

public class HibernateUtil {
    private static SessionFactory sessionFactory ;
    static {
        Configuration configuration = new Configuration();

        configuration.addAnnotatedClass (org.gradle.Person.class);
        configuration.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
        configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");                                
        configuration.setProperty("hibernate.connection.username", "root");     
        configuration.setProperty("hibernate.connection.password", "root");
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        configuration.setProperty("hibernate.hbm2ddl.auto", "update");
        configuration.setProperty("hibernate.show_sql", "true");
        configuration.setProperty(" hibernate.connection.pool_size", "10");

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
} 
like image 23
Deepak Avatar answered Oct 12 '22 02:10

Deepak