Вы находитесь на странице: 1из 2

Bootstrapping Hibernate:-

There are 2 types of configuration file in java:-


1. properties file.

2. xml file.

There are 3 ways of bootstrapping hibernate (writing hibernate configuration information):


1. By properties file

2. BY XML file.

3. Programmetic Approach.

1. First Approach:
Standard file name : hibernate.properties
hibernate.properties :
hibernate.driver_class=
hibernate.url=
hibernate.username=
hibernate.password=

Limitation of properties file:

In properties file, we can not configure mapping resources information (only configuration
information).
We need to add the mapping info programmetically :
Configuration configuration =new Configuration().configure();
----configure() method search for Hibernate.cfg.xml file in classpath (search
only for xml file not properties file).

To configure properties file along with mapping resources:-


Configuration configuration=new Configuration();
configuration.addResouce("path"); // if 10 configuration files are there, we have to
call this method for 10 times.
or
configuration.addClass(Employee.class); //for mappping configuration

Lines of code will be increased. A new file has been added, change the code.

2. Second Approach:
It is recommended to use xml file (Hibernate.cfg.xml placed in classpath)
Configuration configuration =new Configuration().configure();
----configure() method search for Hibernate.cfg.xml file in classpath(src
directory).

If the configuration file is not in src directory, it is in subdirectory of src directory then :
Configuration configuration =new Configuration().configure("path");

When to go for which approach (04. - 38:02):-


1. If you want to leverage existing configuration instead of providing hibernate
configuration as part of your application then you need to go for programmatic
approach.

2. If you are just developing a simple application for experimental shake then you need to
go for properties file rather than writing the xml file.

3. If you are working on an enterprise application or a realistic application development, it


is always recommended to have a strict validation as well you need to for
Hibernate.cfg.xml (because the configuration will not only permit you to put database
configuration even the mapping information can also be dumped as part of it).

Вам также может понравиться