Hibernate: A Quick Tour Prasanna
Hibernate: A Quick Tour Prasanna
Prasanna
Hibernate is a bridge between relational data and Java Objects. It is an object/relational mapping tool for Java environments.
What is ORM??
The term object/relational mapping (ORM) refers to the technique of mapping a data representation from an object model to a relational data model with a SQL-based schema.
In a runtime architecture : where does Hibernate come into picture ??? Answer is -- >>
Hibernate Libraries
dom4j.jar (req) -- Hibernate uses dom4j to parse XML configuration and XML mapping metadata files. CGLIB (req) Hibernate uses code generation library to enhance classes at runtime (in combination with java reflection) Commons Collections, Commons Logging (req) -- These are various utility libraries from the Apache Jakarta Commons Project. ODMG4(req) Hibernate provides an optional ODMG compliant persistent manager interface. EHCache (req) Hibernate can use various cache providers for the second-level cache. Log4j(optional) If commons logging API uses Log4j as the underlying logging mechanism then this is used.
Terminologies:
Transparent Persistence. Dirty Checking. Persistent instances. Transitive persistence . Lazy fetching. Eager fetching (Outer Join Fetching).
Integrating Hibernate
Five core Interfaces that are used in just about every Hibernate application. Using these interfaces one can store and retrieve persistent objects and control transactions. They are -- >>
Limbs of Hibernate
Session Interface. Session Factory Interface Configuration Interface. Transaction Interface. Query and Criteria Interfaces.
Before we go further
Difference between : Managed and Non-Managed Environments.
Non-Managed Environments.
One has to explicitly tell hibernate how to get (or create new ) JDBC Connections. In a non-managed environment an Application is responsible for database connections and Hibernate is part of the application hence it is responsible for the database connections. Eg: Tomcat.
Managed Environments.
A managed environment handles certain cross-cutting concerns, such as application security (authorization and authentication ), connection pooling, and transaction management. In case of a managed Environment an application server exposes connection pool as a data source, an instance of javax.jdbc.Datasource One needs to tell Hibernate where to find the data source in JNDI, by specifying a fully qualified domain name. Eg. JBOSS.
Data-source mapping
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>jdbc/OracleDS</jndi-name> <connectionurl>jdbc:oracle:thin:@localhost:1521:Prasanna</connection-url> <!-- The driver class --> <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> <!-- The login and password --> <user-name>scott</user-name> <password>tiger</password> <min-pool-size>5</min-pool-size> <max-pool-size>20</max-pool-size> <idle-timeout-minutes>0</idle-timeout-minutes> <track-statements/> </local-tx-datasource> </datasources>
/ This is a POJO file from which getters and setters are defined package examples.quickstart; public class Cat { private String id; private String name; private char sex; private float weight; public Cat() { // Hibernate must have this default constructor so that it can // so that it can instantiate them using Constructor.newInstance() // == But do not know why even if u comment it still works === } public String getId() { return id; } private void setId(String id) { this.id = id; }
public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } }
<hibernate-mapping> <class name="examples.quickstart.Cat" table="CAT"> <id name="id" type="string" unsaved-value="NONE" > <column name="CAT_ID" sql-type="char(16)" not-null="true"/> <generator class="uuid.hex"/> </id> <property name="name"> <column name="NAME" sql-type="char(32)" length="32" not-null="true"/> </property> <property name="sex"> <column name="SEX" sql-type="char(1)" length="1" not-null="false"/> </property> <property name="weight"> <column name="WEIGHT" sql-type="int" not-null="false"/> </property> </class> </hibernate-mapping>
<hibernate-configuration> <session-factory>
<property name="connection.datasource"> java:comp/env/jdbc/OracleDS</property> <property name="show_sql">false</property> <property name="dialect"> net.sf.hibernate.dialect.OracleDialect</property> <property name="show_sql">false</property> <mapping resource="Cat.hbm.xml"/>
</session-factory> </hibernate-configuration>
public class HibernateUtil { private static Log log = LogFactory.getLog(HibernateUtil.class); private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { } } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); session.set(null); if (s != null) s.close(); }}