官术网_书友最值得收藏!

Providing a hibernate configuration using the properties file

This is another way to configure hibernate; here, we will create a file with the .properties extension. Usually called hibernate.properties, this file is a replacement for hibernate.cfg.xml. You can use any approach (either cfg.xml or the properties file). However, the properties file is better for startup, and it is the easiest approach to get started quickly.

This is a simpler representation of an XML file. Hibernate searches for the XML file or the properties file at startup to find the configuration in your classpath. We can use any one of these options. You can use both of them at the same time, but this is uncommon because hibernate gives priority to the XML file over properties; the properties file is simply ignored in such cases.

Note

The properties file looks similar to a normal text file, but the content should be in a key/value pair, which is Key=Value.

Here is an example: hibernate.connection.driver_class=com.mysql.jdbc.Driver.

How to do it…

Now, we will create a file called hibernate.properties in our classpath and write the following properties in the file. The following code represents hibernate.cfg.xml in the hibernate.properties file:

…
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/kode12
hibernate.connection.username=root
hibernate.connection.password=root
show_sql=true
hbm2ddl.auto=update
…

How it works…

When we create an instance of the Configuration class, it will look for hibernate.cfg.xml or hibernate.properties in our classpath. If we use a .properties file, it'll get all of the property defined in the file, rather than create a Configuration object.

Note

The difference between an XML and properties file is that, in an XML file, you can directly map classes using the <Mapping> tag, but there is no way to configure it in a properties file. So, you can use this methodology when you use a programmatic configuration.

主站蜘蛛池模板: 交城县| 阳泉市| 天台县| 乌鲁木齐县| 广平县| 和田市| 长寿区| 泸定县| 宿迁市| 唐山市| 宁明县| 七台河市| 南皮县| 凤山县| 前郭尔| 虎林市| 温泉县| 睢宁县| 申扎县| 望奎县| 遂川县| 石渠县| 维西| 寿光市| 怀柔区| 光泽县| 大足县| 抚远县| 左云县| 象山县| 贵德县| 墨江| 久治县| 云阳县| 南城县| 东海县| 连江县| 富锦市| 通渭县| 海晏县| 洪泽县|