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

2019-02-04 06:51发布

问题:

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

回答1:

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.



回答2:

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();



回答3:

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;
    }
} 


回答4:

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. it’s not mandatory to use hibernate.cfg.xml. Just don’t use .configure().

static {
    try {
        Properties prop= new Properties();
        prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
        prop.setProperty("hibernate.connection.username", "root");
        prop.setProperty("hibernate.connection.password", "");
        prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");

        concreteSessionFactory = new AnnotationConfiguration()
       .addPackage("com.concretepage.persistence")
               .addProperties(prop)
               .addAnnotatedClass(User.class)
               .buildSessionFactory();
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }