Est-il obligatoire d'avoir hibernate.cfg.xml fichier de configuration

Je ne veux pas avoir l'hibernate.cfg.xml fichier. Plutôt que je veux faire toute la configuration par le biais de mon code comme indiqué ci-dessous.

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

J'ai essayé l'approche ci-dessus. Mais je suis confronté au problème d'hibernate déclenche une exception en disant:"./hibernate.cfg.xml file missing".

Est-il vraiment obligatoire de conserver le hibernate.cfg.xml fichier?

Merci d'avance

InformationsquelleAutor Sashwat | 2012-01-12