仅使用hibernate.properties文件的Hibernate配置。没有任何hibernate.cfg.xml

时间:2016-03-14 13:22:13

标签: java xml hibernate properties-file

是否可以仅使用hibernate.properties文件配置hibernate而不使用任何hibernate.cfg.xml文件?

我在hibernate.properties文件中定义了必需的属性。

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/dev
hibernate.connection.username=root
hibernate.connection.password=
hibernate.connection.pool_size=1
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.hbm2ddl.auto=update

Hibernate显示:

Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.format_sql=true, hibernate.connection.username=root, hibernate.hbm2ddl.auto=update, hibernate.connection.url=jdbc:mysql://localhost:3306/dev, hibernate.bytecode.use_reflection_optimizer=false, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.connection.pool_size=1}

但它仍然要求cfg.xml

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

1 个答案:

答案 0 :(得分:1)

可能

SessionFactory factory = new Configuration().buildSessionFactory();