Explain how to configure Hibernate.
- Hibernate uses a file by name hibernate.cfg.xml. This file creates the connection pool and establishes the required environment. A file named .hbm.xml is used to author mappings. The bootstrap action is configured by using Configuration interface.
- There are two types of environment to configure hibernate:
1. Managed Environment : The definitions of database operations such as connections, transaction boundaries, security levels. This environment is provided by application servers such as Jboss,Weblogic,Websphere.
2. Non-managed Environment : The basic configuration template is provided by this interface. Tomcat is one of the examples that best supports this environment.
Explain how to configure Hibernate.
Programmatic configuration :- The org.hibernate.cfg.Configuration instance can be instantiated directly by specifying XML mapping documents. If the mapping files are in the classpath, use addResource().
Configuration cfg = new Configuration()
.addResource("Item.hbm.xml")
.addResource("Bid.hbm.xml");
- An alternative way is to specify the mapped class and allow Hibernate to find the mapping document :
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);
-org.hibernate.cfg.Configuration also allows you to specify configuration properties :
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");
- Alternative options include:
- Passing an instance of java.util.Properties to Configuration.setProperties().
- Placing a file named hibernate.properties in a root directory of the classpath.
- Setting System properties using java -Dproperty=value.
- Including <property> elements in hibernate.cfg.xml (this is discussed later).
Hibernate JDBC Properties :hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.connection.pool_size