我不想拥有hibernate.cfg.xml文件。相反,我想通过我的代码完成所有配置,如下所示。是否需要配置hibernate.cfg.xml文件
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();
}
我试过上面的方法。但是我面临的问题是,hibernate抛出一个异常说“./hibernate.cfg.xml file missing
”。
是否真的必须保留hibernate.cfg.xml
文件?
在此先感谢
如果要在控制台上记录sql查询,请使用:configuration.setProperty(“hibernate.show_sql”,“true”); – Deepak 2015-10-12 16:23:50
那么如何指定映射? – digz6666 2016-08-30 05:13:38
通过这种方式:configuration.addAnnotatedClass(org.gradle.Person.class); – Deepak 2016-08-31 04:07:31