是否必须有hibernate.cfg.xml文件进行配置 [英] Is it mandatory to have hibernate.cfg.xml file for configuration
问题描述
我不想有hibernate.cfg.xml文件。而我想通过我的代码做所有的配置如下所示。
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();
}
我已经尝试了上述方法。但我面临的问题,hibernate抛出一个异常说 ./ hibernate.cfg.xml文件丢失
。
I have tried the above approach. But I am facing the problem where hibernate throws an exception saying "./hibernate.cfg.xml file missing
".
保存 hibernate.cfg.xml
文件真的是强制性的吗?
Is it really mandatory to keep the hibernate.cfg.xml
file?
>
推荐答案
我相信这是因为您 configure()
c $ c>配置对象。尝试删除它,并且hibernate将不会查找不存在的文件。基本上你通过属性
对象设置所有必需的属性,所以没有什么真正需要告诉Hibernate寻找 hibernate.cfg.xml
文件,这正是 configure()
方法。
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.
这篇关于是否必须有hibernate.cfg.xml文件进行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!