First, write the following code:
This will create an instance of the Configuration
class using hibernate.cfg.xml
or hibernate.properties
, whichever is found in the classpath.
Provide the following mapping files to the configuration:
You can use an alternate way, as shown in the following code:
We can also provide a direct mapping using the class, as shown in the following code:
This will also look for Department.hbm.xml
.
We can also set a custom property. To set up the custom property, use the following method:
For example, consider the following code:
To set up multiple properties using the properties object, execute the following code:
Here is an example:
To read the mapping from the URL, you can use the following code:
To read the mapping from the XML file, you can use the following code:
When we select the programmatic configuration option, the Configuration
class is very important. Using the instance of the Configuration
class, we will build a SessionFactory
object, as shown in the following code:
When the preceding code is executed, it creates a SessionFactory
object using a .properties
or .cfg
file or whichever source is provided to create the configuration.