Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Configuration In Hibernate

Configuration: In  Hibernate Configuration represents the mappings of Java Types to Database.
All the mappings have been parsed by configuration object Hibernate uses run time reflection to determine the persistent properties of a class.The objects which are to be persisted are define on mapping document, which describes persistent fields and associations as well as sub classes and proxies of the persistent object.

The instance of Configuration can be obtained directly by instantiating it.
org.hibernate.cfg.Configuration.

  Basic Configuration:             Configuration cfg=new Configuration();
                                                  cfg.configure();



 While configuring  we can tell hibernate to add either hbm files(Resources) or classes.
 Hibernate requires mapping files information for mapping Java classes(POJO) to tables in db.For that we are adding mapping files information to configuration object as shown below.

 Add Resource:                       Configuration cfg=new Configuration();
                                                 cfg.addResource("Project.hbm.xml");
                                                 cfg.addResource("Allocation.hbm.xml");
                                                 cfg.onnfigure();


If we add classes to the configuration object then hibernate get the mapping files information from the pat specified by classes, like org/discovery/Project.hbm.xml and org/discovery/Allocation.hbm.xml

Add Class:                              Configuration cfg=new Configuration();
                                                 cfg.addClass("org.discovery.Project.class");
                                                 cfg.addClass("org.discovery.Allocation.class"); 
                                                 cfg.configure();

Configuration instance can also allow to specify configuration properties.

Additional Properties:           Configuration cfg=new Configuration();
                                                cfg.setProperty("hibernate.dialect","org.hibernate.dialect.OracleDialect");
                                                cfg.setProperty("hibernate.connection datasource","java:comp/env/hib/test");


Though Configuration instance is a very expensive in terms of time and space we should create only one instance per application.

If we use adding classes and mapping files directly to the configuration instance in java code then further addition of resources will create headache to the developers. So the preferred way is create configuration instance and configure it in java code and add mapping files on xml file.Hence for any further modifications we couldn't touch the source code and do additions or deletions directly on xml. We will see in later section how the cfg.xml file can be created.
                                                  


                                                                                                                                  Vissu
                                           
                                                     



This post first appeared on Java Spring Hibernate, please read the originial post: here

Share the post

Configuration In Hibernate

×

Subscribe to Java Spring Hibernate

Get updates delivered right to your inbox!

Thank you for your subscription

×