You are on page 1of 28

1

1What is Hibernate? Hibernate is an open source project to provide a powerful, high performance object/relational persistence and query service. Hibernate generates SQL for you relieves you from manual JDBC result Ser handling and object conversion, and keeps you application portable to all SQL databases. Hibernate provides transparent persistence, the only requirement for a persistent class is a no argument constructor. Hibernate offers sophisticated query options you can write plain SQL, object-oriented HQL(Hibernate Query Language), or create programmatic Criteria an example queries(QBE). Hibernate can optimize object loading all the time, with various fetching and caching opetions. Hibernates goal is to reliev the developer from 95 percent of common dta persistence related programming tasks, compared to manual coding with SQL and the JDBC API.

Hibernate architecture has three main components: Connection Management Hibernate Connection management service provide efficient management of the Databaseconnections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection. Transaction management: Transaction management service provide the ability to the user to execute more than one database statements at a time. Object relational mapping: Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query. An application using Hibernate contains the following components Hibernate Configuration file Either this is an XML file called hibernate.cfg.xml or a properties file called hibernate.properties. Properties defined in the configuration file specify the following Database properties Entity classes to be managed by Hibernate SQL dialect- how to convert HSQL to native SQl Other properties related to caching etc. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="hibernate.connection.username">hr</property> <property name="hibernate.connection.password">hr</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="user.hbm.xml"/> </session-factory> </hibernate-configuration>

2
Entity Classes These classes are nothing but POJO (plain Old Java Object). Its field are mapped to columns in a row in table. Either you can declare these classes in mapping file or use annotations, to declare metadata. Each object of this class must have an id (Primary key0, which is used to uniquely identify the object import java.util.Date; public class User { private String uname,email,mobile; private Date dob; public User() { } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public Date getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } } Mapping files These files are xml files used to specify object Relation mapping (ORM) these files are to be listed in hibernate configuration file or added to configuration object using addResource() method of configuration object. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping3.0.dtd"> <hibernate-mapping> <class name="user.User" table="Users"> <id name="uname" column="User_name"> <generator class="assigned"/> </id> <property name="email" column="User_email" type="string" length="20"/> <property name="mobile" column="User_mobile" type="string" length="10"/> <property name="dob" column="User_Dob" type="timestamp" /> </class> </hibernate-mapping> Configuration object An instance of configuration represents an entire set of mapping of an applications. Java types to an SQL database. The Configuration is used to build an (immutable)SessionFactory. The Mapping are compiled from various XML files Session Factory object A thread safe(immutable) cache of compiled mappings for a simple database. A factory for Session and a client of ConnectionProvider. Might hold an optional(second-level) cache of data that is resalable between transactions, at a process-level or cluster-level Session object A single-treaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection . Factory for Transitions. Holds a mandatory(first-level)cache of persistent objects used when navigating the object graph or looking up objects by identifier . All operations related to persistence are done using methods in session object. Session objects is created from Session Factory object, which in turn is created from Configuration class AddUser.jap import java.util.Date; import org.hibernate.*; public class AddUser { public static void main(String[] args) throws Exception { Configuration cfg=new Configuration().configure(); SessionFactory sf=cfg.buildSessionFactory(); Session session=sf.openSession(); User u=new User(); u.setUname("abc"); u.setEmail("abc@gmail.com"); u.setMobile(1234567890); u.setDob(new Date()); session.beginTransaction(); session.save(u); session.getTransaction().commit(); session.close(); sf.close(); } }

3
Persistent Objects and collection Short-lived, single threaded objects containing persistent state and business function. These might be ordinary JavaBeans/POLJS, the only special thing about them is that they are currently associated with (Exactly one )i Session . As soon as the session is closed, they will be detached and free to use in any application layer(e.g directly as data transfer object to and from presentation). Transient and detached objects and collections instance of persistent classes those are not currently associated with a session. They may have been instantiated by the application and not(yet) persisted or they may have been instantiated by a closed session. Transaction A single-threaded, short-lived object used by the application to specify atomic units of work, abstracts application from underlying JDBC,JTA or CORBA transaction. A Session might span several Transaction in some cases. However, transaction demarcation, either using the underplaying API or Transaction is never optional. import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class ListUsers { public static void main(String[] args) throws Exception { Configuration cfg=new Configuration().configure(); SessionFactory sf=cfg.buildSessionFactory(); Session session=sf.openSession(); Query q=session.createQuery("from User"); List l=q.list(); for(Object o:l){ User u=(User) o; System.out.println(u.getUname()+":"+u.getEmail()+":"+u.getMobile()+":"+u.getDob()); } } }

Methods of configuration information user one of the following methods Method Description Configure() Use the mappings and properties specified int eh application resource named hibernate.cfg.xml Confgirue(File Use the mapints and properties specified int eh given application file configfile) Confgirue(String Use the mappings the properties specified in the given application resoruce) Configuret(URL url Use the mappings and properties specified int eh given document. Or

Configuration cfg=new Configuration();

cfg.configure(hibernate.cfg.xml);
Addition mapping fiels
You can add mapping document using hibernate configuration file. An alternative (sometimes better) way is to specify the mapped class and let Hibernate find the mapping document for you. You can use one of the following methods of configuration object to load mapping files. Method addClass(Class class) addDirectory(File dir) addFile(File xmlFile addFile(String xmlFile) addResouce(String Description Read a mapping as an application resource using the convention that a class named st.Title is mapped by a file st/title.hml.xml which can be resolved as a classpath resource. Read all maping documents from a directly tree. Read mappings from a particular XML File Read mappings from a particular XML file Read mappings from the specified resource(file

4
resource) Configuratio cfg=new Configuration().addResource(title.hbm.xml).addResource(author.hbm.xml); Configuration cfg=new Configuration().addClass(Title.class).addClass(Author.class); JDBC connection properties Usually, you want to have the SessionFactory to be created and pool JDBC connections. As soon as you do something that requires access to the database, a JDBC connection will be obtained from the pool For this to work, we need to pass some JDBC connection properties to Hibernate. hibernate.connection.driver_class oracle.jdbc.OracleDriver hibernate.connection.url Jdbc URL hibernate.connection.username Database user nam hibernate.connection.password Database user password hibernate.connection.pool_size

SQL Dialects

You should always set the hibernate.dialect property to the correct org.hibernate.dialect.Dialect subclass for your database. DB2 - org.hibernate.dialect.DB2Dialect HypersonicSQL - org.hibernate.dialect.HSQLDialect

Informix - org.hibernate.dialect.InformixDialect Ingres - org.hibernate.dialect.IngresDialect Interbase - org.hibernate.dialect.InterbaseDialect Pointbase - org.hibernate.dialect.PointbaseDialect PostgreSQL - org.hibernate.dialect.PostgreSQLDialect Mckoi SQL - org.hibernate.dialect.MckoiDialect Microsoft SQL Server - org.hibernate.dialect.SQLServerDialect MySQL - org.hibernate.dialect.MySQLDialect Oracle (any version) - org.hibernate.dialect.OracleDialect Oracle 9 - org.hibernate.dialect.Oracle9Dialect Progress - org.hibernate.dialect.ProgressDialect FrontBase - org.hibernate.dialect.FrontbaseDialect SAP DB - org.hibernate.dialect.SAPDBDialect Sybase - org.hibernate.dialect.SybaseDialect Sybase Anywhere - org.hibernate.dialect.SybaseAnywhereDialect

Understanding Hibernate O/R Mapping


Hibernate mapping documents are simple xml documents. Here are important elements of the mapping file:. 1. <hibernate-mapping> element The first or root element of hibernate mapping document is <hibernate-mapping> element. Between the <hibernate-mapping> tag class element(s) are present. 2. <class> element The <Class> element maps the class object with corresponding entity in the database. It also tells what table in the database has to access and what column in that table it should use. Within one <hibernate-mapping> element, several <class> mappings are possible. <id> element The <id> element in unique identifier to identify and object. In fact <id> element map with the

3.

5
primary key of the table. In our code : <id name="id" type="long" column="ID" > primary key maps to the ID field of the table CONTACT. The attributes of the id element are: name: The property name used by the persistent class. column: The column used to store the primary key value. type: The Java data type used. unsaved-value: This is the value used to determine if a class has been made persistent. If the value of the id attribute is null, then it means that this object has not been persisted. 4. <generator> element The <generator> method is used to generate the primary key for the new record. Here is some of the commonly used generators * Increment - This is used to generate primary keys of type long, short or int that are unique only. It should not be used in the clustered deployment environment. * Sequence - Hibernate can also use the sequences to generate the primary key. It can be used with DB2, PostgreSQL, Oracle, SAP DB databases. * Assigned - Assigned method is used when application code generates the primary key. 5. <property> element The property elements define standard Java attributes and their mapping into database schema. The property element supports the column child element to specify additional properties, such as the index name on a column or a specific column type.

Persistent classes

Persistent classes are classes in an application that implement the entities of the business problem(e.g. customer and Order in an E-commerce application). Hibernate works best if these classes follow some simple rules, also known as the plain OLD Java Object(POJO) programming model, However none of these rules are hard requirements. Indeed, Hibernate3 assumes very little about the nature of your persistent objects. All persistent classes must have a default constructor. All persistence classes must have an identifier property. This property maps to the primary key column of a database table. You have to override the equals() and hashCode() methods if you o Intend to put instances of persistent classes in a set and o Intend to use reattachment of detached instances

Session interface
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.) The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states: transient: never persistent, not associated with any Session persistent: associated with a unique Session detached: previously persistent, not associated with any Session The lifecycle of the session is bounded by the beginning and end of a logical transaction Transient instances may be made persistent by calling save(),persist() or saveOrUpdate() Persistent instances may be made transient by calling delete() Any instance returned by a get() or load() method is persistent Detached instances may be made persistent by calling update(),svaeOrUpdate(),lock() or replicate() The state ofa transient or detached instance may also be make persistent as a new persistent instance by calling merge() Methods save() and persist() result in a SQL insert, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush times and also result in an sql UPDATE.

6
Method saveOrUpdate() and replicate() result in either an INSERT or an UPDATE. Begin a unit of work and return the associated

Transaction beginTransaction() void cancelQuery() Connection close() boolean contains(Object object) Criteria createCriteria(Class persistentClass) Query createFilter(Object collection, String queryString) Query createQuery(String queryString) SQLQuery createSQLQuery(String queryString) void delete(Object object) void evict(Object object) void flush() Object get(Class clazz, Serializable id Query getNamedQuery(String queryName) SessionFactory getSessionFactory() Transaction getTransaction() boolean isOpen() boolean isDirty() Object load(Class theClass, Serializable id) Object merge(Object object) void persist(Object object) void refresh(Object object) Serializable save(Object object void saveOrUpdate(Object object)

Transaction object.
Cancel the execution of the current query. End the session by releasing the JDBC connection and cleaning up. Check if this instance is associated with this Session. Create a new Criteria instance, for the given entity class, or a superclass of an entity class. Create a new instance of Query for the given collection and filter string. Create a new instance of Query for the given HQL query string. Create a new instance of SQLQuery for the given SQL query string. Remove a persistent instance from the datastore. Remove this instance from the session cache. Force this session to flush. Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. Obtain an instance of Query for a named query string defined in the mapping file. Get the session factory which created this session. Get the Transaction instance associated with this session. Check if the session is still open. Does this session contain any changes which must be synchronized with the database? In other words, would any DML operations be executed if we flushed this session? Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. Copy the state of the given object onto the persistent object with the same identifier. Make a transient instance persistent. Re-read the state of the given instance from the underlying database. Persist the given transient instance, first assigning a generated identifier. Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking). Set the cache mode. Update the persistent instance with the identifier of the given detached instance.

void setCacheMode(CacheMode cacheMode void update(Object object)

7 new save() saveOr Update( ) evict() close() clear() Transient delete() Persistent
update() saveOtrUpdate() lock() garbage

get() load()

Detached

garbage

Making object persistent Newly instantiated instance of an entity are considered transient by Hibernate. We can make a transient instance persistent by a associating it with a session as follows Person person=new Person() person.setName(Siva Prakash); session.save(person);

Loading an object
Each load(..)method requires objects primary key as an identifier Each load(..) method also requires which domain class or entity name to use to find the object with the id The returned object, which is returned as object type, needs to be type-case to the domain class load() vs.get() Use the load() method if you are sure that the object exists. Load() method will throw an exception if the unique id is not found in the database. If you are not sure that the object exists, then use one of the get() methods, which returns null if object is not found in the database Updating object Updating object Hibernate automatically manages any changes made to the persistent objects If a property changes on a persistent object, Hibernate session will perform the change in the database You can force Hibernate to commit all of its changes using flush() method. You can also determine if the session is dirty through isDirty() method. Transaction tx=session.beginTransaction(); Person person=(Person)session.load(Person.class,new Integer(1)); person.setName(Siva Prakash); tx.commit(); Refreshing Objects Use refresh() method to refresh objects from their database representations in cases where there is a possibility of persistent object is not in sync. With the database representation. Scenarios you might want to do this; you hibernate application is not the only application working with this data, you applications executes some SQL directly against the database You database uses triggers to populate properties on the object Deleting objects Use delete() method to remove an object from the database. Your application might still hold a reference to a deleted object. Its best to think of delete() as making a persistent instance transient. Life-cycole Operations and SQL commands save() and persist() result in an SQL INSERT delete() result in an SQL DELETE update() or merge() result in an SQL UPDATE changes to persistent instances are detected at flush time and also result in an SQL UPDATE saveOrUpdate() and replicate() result in their an INSERT or an UPDATE Cascading Operations lifecycle operations can be cascaded

8
cascading configuration flag(specified in the mapping update,evict,replicate,lock,refresh <one-to one nameperson cascade=persist,.delete,lock/> file)-persist,merge,delete,save-

Transaction interface it allows the applications to define units of work, while maintaining abstraction from the underlying transaction implementation(ex.JTA<JDBC) A transaction is associated with a session and is usually instantiated by a class to session.beginTransaction(). A single session might span multiple transactions. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time Method Meaning void begin() Begin a new transaction. Void commit() Flush the associated Session and end the unit of work(unless we are in FlushMode.NEVER) Boolean isActive() Is this transaction still active? Again, this only return information in relation to the local transaction, not the actual underlying transactions void rollback() boolean wasComitted() Boolean wasRolledBack() Force the underlying transaction to rollback Check if this transaction was successfully commited Was this tgransaction rololed back or set to rollback only? This only account for actions initiated from this local transaction

Generation of ID Mapping class must declare the primary key column of the database table. The <id> element defines the mapping from the property to the primary key column <id name=propertyName type=typename column=column_name> <generator classe=generatorclass/> </id> Thye optional <generator> child elements names a java class used to generator unique identifiers for instances of the persistent class. If any parameters are required to configure or initialize the generator instance, they are passed using the <param> element. <id column="PersonID" name="pid" > <generator class="hilo"> <param name="table">p_id</param> <param name="column">hi_value</param> </generator> </id>

All generators implemented the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface; some applications may chose to provide their own specialized implementation. How ever, Hibernate provodes a range of built-in implementations. Generator increment identity sequence Description It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It should not the used in the clustered environment. It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int. The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection. The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence. The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32. It uses a database-generated GUID string on MS SQL Server and MySQL. It picks identity, sequence or hilo depending upon the capabilities of the underlying database. lets the application to assign an identifier to the object before save() is called. This is the default

hilo

seqhilo uuid guid native assigned

9
strategy if no <generator> element is specified. select foreign retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value. uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.

For a table with a composite key, you may map multiple properties of the class as identifier properties. The <composite-id> element accepts <key-propertie> property mapping as child elements. Composite ID For a table with a composite key, you may map multiple properties of the class a indentified properties. The <composite-id> element accepts <key-property> property mappings as child elements. Person.java

import java.io.Serializable; import java.util.Date; public class Person implements Serializable{ private String pname,email; public Person() { } . }
Address.java

import java.io.Serializable; public class Address implements Serializable { private String city,state,country; private Person person; public Address() { } . }

Person.hbm.xml

package comID; import org.hibernate.HibernateException; <hibernate-mapping> import org.hibernate.Session; <class name="Address" table="PersonAddress"> import org.hibernate.Transaction; <composite-id class="Person" name="person"> import org.hibernate.cfg.Configuration; <key-property name="pname"/> public class NewMain { <key-property name="email"/> public static void main(String[] args) { </composite-id> // TODO code application logic herename="city" type="string"/> <property column="City" length="20" Configuration cfg = new Configuration().configure(); <property column="State" length="25" name="state" type="string"/> Session <property session column="Country" = cfg.buildSessionFactory().openSession(); length="25" name="country" type="string"/> </class> session.beginTransaction(); </hibernate-mapping> Transaction transaction = null; try { transaction = session.beginTransaction(); Person person = new Person("Siva Prakash", "siva@gmail.com"); Address address = new Address( "vizag", "AP","India",person); session.save(address); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }

10

Basic value types The built-in basic mapping types may be roughly categorized into the following categories. Datatype Description Integer,long,short,float,double,character,byte, Type mapping from java primitive or wrapper classes to Boolean,yes_no, true_false appropriate sql column types String A trype mapping from a java.lang.String to VARCHAR or VARCHAR2 Date,time,timestamp Type mappings from java.util.Date and itssubclassess to SQL types DATE, TIME and TIMESTAMP Calendar,clender_date Type mappings from java.utilCalender to SQL types timestamps and DATE Locale, timezone, currency Type mappings from java.util.Locale, java.util.TimeZone and java.util.Cuirrency to VARCHAR or VARCHAR2. class A type mapping from java.lang.Class or varchar2. A class is mapped toits fuly qualified name binary Maps byte arrays to an appropriate SQL binary types text Maps long java strings to a SQL CLOB or TEXT type. Serializable Maps serializable java types to an appropriate SQL binary type clob,blob Type mappings for the JDBC classes are java.sql.Clob nad java.sql.BLOB Note: unique identifier of entities and collections may be of nay basic type except binary, blob and clob. Component Mapping

A component is a contained object that is persisted as a value type and not an entity reference. The term "component" refers to the object-oriented notion of composition and not to architecturelevel components. Student.java
package compo; public class Student implements java.io.Serializable { private long studentId; private String studentName; private Address studentAddress; public Student() { //create a property } Address.java

package compo; public class Address implements java.io.Serializable { <hibernate-mapping> private String street, city,state,zipcode; <class name="compo.Student" table="STUDENT"> <id name="studentId" column="STUDENT_ID"> public Address() { type="long" } . <generator class="native" /> </id> //create property <property name="studentName" type="string" not-null="true" column="STUDENT_NAME" /> }
<property name="street" column="ADDRESS_STREET" type="string" length="250" /> Student.hbm.xml <component name="studentAddress" class="compo.Address"> <property name="city" column="ADDRESS_CITY" type="string" length="50" /> <property name="state" column="ADDRESS_STATE" type="string" length="50" /> <property name="zipcode" column="ADDRESS_ZIPCODE" type="string" length="10" /> </component> </class> </hibernate-mapping>

11

Person

HAS A

Address

package compo; import org.hibernate.*; import org.hibernate.cfg.Configuration; public class Main { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); Session session = cfg.buildSessionFactory().openSession(); session.beginTransaction(); Transaction transaction = null; try { transaction = session.beginTransaction(); Address address = new Address("MADHAVDHARA", "vizag", "AP", "530007"); Student student = new Student("Siva Prakash", address); session.save(student); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }

Inheritance Mapping Hibernate supports the three basic inheritance mapping strategies: Table per class hierarchy Table per subclass Table per concrete class We use the following three Entities to understand how inheritance can be implemented in Hibernate. student.java

package student; public class Students { Science.java private int sno; package student; private String name; public class Science extends Students { public Students() { } private int pmarks,tot; public Students(int sno, String name) { public Science() { } this.sno = sno; public Science(int sno, String name, int pmarks, int tot) { this.name = name; super(sno,name); this.pmarks = pmarks; this.tot = tot; } } //create a properties of sno,name //create a properties of pmarks and tot; } }

Arts.java
package student; public class Arts extends Students { private int tot; public Arts() { } public Arts(int sno,String name,int tot) { super(sno,name); public int getTot() { return tot; } public void setTot(int tot) { this.tot = tot; } }

this.tot = tot;

12

tableperclasshierarchy.hbm.xml

<hibernate-mapping> <class name="student.Students" table="AllStudents"> <id name="sno" length="3"> <generator class="native"/> </id> <discriminator column="Stype" length="10" type="string"/> <property name="name" column="name" type="string" length="10"/> <subclass discriminator-value="Science" name="student.Science"> <property name="pmarks" type="int" length="10"/> <property name="tot" type="int" length="10"/> </subclass> <subclass discriminator-value="Arts" name="student.Arts"> <property name="tot" type="int" length="10"/> </subclass> </class> </hibernate-mapping>

SQL> select * from allstudents; SNO STYPE NAME PMARKS TOT ---------------------------- ------------------1 Science rams 110 250 2 Arts krish 575

Table per sub class In this inheritance is represented using foreign key association. Every class/subclass that has persistent properties(including abstraction classes) has its own table. Table related to subclasses are related to super class table using foreign foreign key in subclass table. Schema is normalized Queries require a join across many tables Performance may be unacceptable for complex class hierarchies. <hibernate-mapping>

<class name="student.Students" table="SchoolStudents"> <id name="sno" length="4"> <generator class="sequence"> <param name="sequence">tpersubclass_seq </param> </generator> </id> <property name="name" column="name" type="string" length="10"/> <joined-subclass name="student.Science" table="Science"> <key column="sno"/> <property name="pmarks" type="int" length="10"/> <property name="tot" type="int" length="10"/> </joined-subclass> <joined-subclass table="Arts" name="student.Arts"> <key column="sno"/> <property name="tot" type="int" length="10"/> </joined-subclass> </class> </hibernate-mapping>
create sequence tpersubclass_seq start with 2000 increment by 1 nocache;

13

SQL> select * from Schoolstudents; SNO NAME --------------------------------1 rams 2 krish SQL> select * from science; SNO PMARKS TOT ---------- ---------- ---------1 110 250 SQL> select * from arts; SNO TOT ---------- ---------Table per concrete class 2 575

A table

is created with all the properties(including inherited for each concreate class. Queries against superclass must be executed on several table related to concrete subclasses. Queries against subclasses perform well A change to super class property needs a change to subclass tables Difficult to implement integrity constraints

<hibernate-mapping> <class name="student.Students" table="OnlyStudents" abstract="true"> <id name="sno" length="3"> <generator class="native"/> </id> <property name="name" column="name" type="string" length="10"/> <union-subclass name="student.Science" table="OnlyScience"> <property name="pmarks" type="int" length="10"/> <property name="tot" type="int" length="10"/> </union-subclass> <union-subclass table="OnlyArts" name="student.Arts"> <property name="tot" type="int" length="10"/> </union-subclass> </class> </hibernate-mapping>
SQL> select * from onlyscience; SNO NAME PMARKS TOT ---------- ---------- ---------- ---------41 rams 110 250 SQL> select * from onlyarts; SNO NAME TOT ---------- ---------- ---------42 krish 575

Associations

Relationships between entities form the crux of the relational approach. This must be well reflected by an ORM framework. In the world of the object-oriented approach, relations between objects are represented by associations. So the task of any ORM framework is to create a mapping between the relationships and the associations. This mapping is known as an association. The term is the same as that of the object oriented approach. Hence this discussion will focus on both the life-cycle states of persistent objects and associations. The first section will deal with the life-cycle of the persistent object. In the second section I will discuss associations. The third section will put the concepts to work, as I will extend the application which was being developed during previous parts to include one more table with a

14
many to one relationship with the order table. Now that the agenda has been disclosed, l will get right into the topic.
Relationship between entities can be classified into one-to-many/nay-to-one and many to- many. Employee.java Relationship may be unidirectional, where only one entity references the other, order bidirectional, where public class Employee { each entity references the other. private int id; Unidirectional one-to-one private LibraryMemebership membership;

package onetoone;

private String name;

In } this there is one-to-one relational between entities and only one entity references the other. relationship is represented by a foreign LibraryMemebrship.java key in a table.

//create a property of id, string memebrship

The

package onetoone; <one-to-one name="propertyName" (1) public class LibraryMemebership { class="ClassName" (2) private int id; cascade="cascade_style" (3) private String type; constrained="true|false" (4) //create a property of id,type fetch="join|select" (5) } property-ref="propertyNameFromAssociatedClass" (6) UniOneToOneDemo.java access="field|property|ClassName" (7) package onetoone; import java.util.List; formula="any SQL expression" (8) import org.hibernate.Session; /> import org.hibernate.cfg.Configuration; (1) name: The name of the property. (2) classclass (optional - defaults to the property type determined by reflection): The name of the associated public UniOneToOneDemo { class. public static void main(String[] args) throws Exception { (3) cascade (optional) specifies which operations should be cascaded from the parent object to the // TODO code application logic here associated object. Configuration cfg = new Configuration().configure(); (4) constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table //cfg.addResource("onttoone/uni_onetoone.hbm.xml"); references the table of the class. This option affects the order in which save() and delete() are Session session = associated cfg.buildSessionFactory().openSession(); cascaded, and determines whether the session.beginTransaction(); association may be proxied (it is also used by the schema export tool). Employee e = new Employee(); (5) fetch (optional - defaults to select): Chooses between outer-join fetching or sequential select fetching. e.setName("prakash"); (6) property-ref: (optional) The name of a property of the associated class that is joined to the primary key of LibraryMembership lm = newkey LibraryMembership(); this class. If not specified, the primary of the associated class is used. lm.setType("Normal"); (7) access (optional - defaults to property): The strategy Hibernate should use for accessing the property value. e.setMembership(lm); session.save(e); (8) formula (optional): Almost all one to one associations map to the primary key of the owning entity. In session.getTransaction().commit(); the rare case that this is not the case, you may specify a some other column, columns or expression to join //retrieve employees on using an SQL formula. List listemployees = session.createQuery("from Employee e").list(); for (Object o : listemployees) { e = (Employee) o; System.out.println(e.getName()+":"+e.getMembership().getType()); } session.close(); } } uni_onetoone.hbm.xml <hibernate-mapping> <class name="onetoone.Employee" table="Employee"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one cascade="all" class="onetoone.LibraryMembership" name="membership" unique="true"/> </class> <class name="onetoone.LibraryMembership" table="LibraryMembership"> <id name="id"> <generator class="native"/> </id> <property name="type"/> </class> </hibernate-mapping>

15

Employee.java package by_onetoone; public class Employee { private int id; private String name; private LibraryMembership membership; //create a property } LibraryMembership.java package by_onetoone; public class LibraryMembership { private int id; private String type; private Employee employee; //create a property } OneToOneDemo.java package by_onetoone; import java.util.List; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class OneToOneDemo { public static void main(String[] args) throws Exception { // TODO code application logic here Configuration cfg = new Configuration().configure(); //cfg.addResource("onttoone/uni_onetoone.hbm.xml"); Session session = cfg.buildSessionFactory().openSession(); session.beginTransaction(); Employee e = new Employee(); e.setName("siva"); LibraryMembership lmember = new LibraryMembership(); lmember.setType("Normal"); Bidirectional One-to-One e.setMembership(lmember); lmember.setEmployee(e); In bidirectional one-to-one association, two entities reference each other with a property in each session.save(e); session.getTransaction().commit(); //retrieve employees List listemployees = session.createQuery("from Employee").list(); for (Object o : listemployees) { e = (Employee) o; System.out.println(e.getName()+":"+e.getMembership().getType()); } List lismembership = session.createQuery("from LibraryMembership").list(); LibraryMembership l; for (Object o : lismembership) { l = (LibraryMembership) o; System.out.println(l.getId()+":"+l.getEmployee().getName()); } session.close(); } }

16

Department.java package manytoone; import java.util.HashSet; import java.util.Set; public class Department { private int id; private String name; private Set<Employee> employees=new HashSet<Employee>(); //create a progperty of the id,name,employees; } Employee.java package manytoone; public class Employee { private int id; private String name; <hibernate-mapping> private Department department; <class name="by_onetoone.Employee" table="Employee"> //create property of id,name,department <idaname="id"> } <generator class="native"/> manytoone.hbm.xml </id> <hibernate-mapping> <property name="name"/> <class name="manytoone.Employee" table="Employees"> <many-to-one cascade="all" class="by_onetoone.LibraryMembership" <id name="id"> name="membership" unique="true"/> <generator class="native"/> </class> </id> <class name="by_onetoone.LibraryMembership" table="LibraryMembership"> <property name="name"/> <id name="id"> <many-to-one cascade="all" class="manytoone.Department" <generator class="native"/> name="department"/> </id> </class> <property name="type"/> <class name="manytoone.Department"> <one-to-one name="employee" property-ref="membership"/> <id name="id"> </class> <generator class="native"/> </hibernate-mapping> </id> Bidirectional one-to-many and many-to-one <property name="name"/> In this <set one entity is on one side and another entity is on many side. One side entity contains a collection of inverse="true" name="employees"> many-side entities. Many-side entity contains a reference to one-side entity. The relationship is represented <key column="departments"/> with a foreign key in manyside table. <one-to-many class="manytoone.Employee"/> </set> </class> </hibernate-mapping>

17

package manytoone; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.Configuration; public class ManyToOneDemo { public static void main(String[] args) throws Exception { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); Employee e1 = new Employee(); e1.setName("abc"); Employee e2 = new Employee(); e2.setName("xyz"); Department d = new Department(); E1 d.setName("Testing"); e1.setDepartment(d); departement e2.setDepartment(d); E d.getEmployees().add(e1); 2 d.getEmployees().add(e2); session.save(d); session.save(e1); session.getTransaction().commit();

session.save(e2);

List lemp = session.createQuery("from Employee e").list(); System.out.println(lemp.size()); for(Object o:lemp){ e1=(Employee)o; System.out.println(e1.getId()+":"+e1.getName() +":"+e1.getDepartment().getName()); } List ldept = session.createQuery("from Department d").list(); for(Object o:ldept){ d=(Department)o; for(Employee emp:d.getEmployees()) System.out.println(emp.getId()+":"+emp.getName()+":"+emp.getName()); } session.close(); } }

18

Bidirectional many-to-many Bidirectional many-to-many is represented with a join table. Join table contains two columns- one references firt entity and another references second table.

package manytomany; import java.util.HashSet; import java.util.Set; public class Employee { private int id; private String name; private Set<Project> projects=new HashSet<Project>(); . //create a property of id,name,projects }

package manytomany; import java.util.HashSet; manytomany.hbm.xml import java.util.Set; <hibernate-mapping> public class Project { <class name="manytomany.Employee"> private int id; <id name="id"> private String title; <generator class="native"/> private Set<Employee> employees=new HashSet<Employee>(); </id> .<property length="10" name="name"/> //create a property ofname="projects" id,title,employees <set cascade="all" table="EmployeeProjects"> } <key column="EmployeeId"/>

<many-to-many class="manytomany.Project" column="projectid"/> </set> </class> <class name="manytomany.Project"> <id name="id"> <generator class="native"/> </id> <property length="20" name="title"/> <set inverse="true" name="employees" table="EmployeeProjects"> <key column="projectid"/> <many-to-many class="manytomany.Employee" column="employeeid"/> </set> </class> </hibernate-mapping>

19

import java.util.List; import org.hibernate.*; import org.hibernate.cfg.Configuration; public class ManyToManyDemo { public static void main(String[] args)throws Exception { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); Employee e=new Employee(); e.setName("abc"); Employee e1=new Employee(); e1.setName("xyz"); Project p=new Project(); p.setTitle("online shopping"); e.getProjects().add(p); Title.java e1.getProjects().add(p); package query; publicp.getEmployees().add(e); class Title { p.getEmployees().add(e1); private int titleid; private String title; session.save(e); private int price; session.save(e1); private Subject subject; session.getTransaction().commit(); //CREATE a property of titleid,title,price,subject public String toString() { return titleid+":"+title+":"+price+":"+subject.getSubcode(); List } elst=session.createQuery("from Employee e").list(); for(Object o:elst){ } e=(Employee)o; ETitle.java System.out.println(e.getName()); package query; for(Project proj:e.getProjects()){ public class ETitle extends Title{ System.out.println("\t:"+proj.getTitle()); private String weburl; } a property of weburl //create } public String toString() { session.close(); return super.toString()+":"+weburl; } } } } Subject.java Hibernate Query language package query; import java.util.HashSet; Hibernate is equipped with an extremely powerful query language that(quit intentionally) looks import java.util.Set; very much like SQL. { HWL is fully object-oriented, understanding notions like inheritance, public class Subject private String subcode; polymorphism and association. private String subname; private Set<Title> titles=new HashSet<Title>(); Queries are case-insensitive, except for names of java classes and properties. } //create a property of subcode,subname,title

20

<hibernate-mapping> catalog.hbm.xml <class name="query.Title" table="Titles"> <id name="titleid"> <generator class="native"/> </id> <property name="title" length="15"/> <property name="price" type="integer"/> <many-to-one cascade="all" class="query.Subject" column="subcode" name="subject"/> <joined-subclass name="query.ETitle" table="ETITLE"> <key column="titleid"/> <property name="weburl"/> </joined-subclass> </class> <class name="query.Subject" table="Subjects"> <id name="subcode"> <generator class="assigned"/> </id> <property name="subname"/> <set inverse="true" name="titles"> <key column="subcode"/> <one-to-many class="query.Title"/> </set> </class> </hibernate-mapping>

21

package query; import org.hibernate.Session; Expressions in HQL import org.hibernate.cfg.Configuration; The public following are the available operatiors functions and expression in HQL class HQLDate { public static void main(String[] args) throws Exception { Mathematical operators +,-,*,/. Configuration cfg = new Configuration().configure(); Session session = cfg.buildSessionFactory().openSession(); Binary comparison operators =,>=,<=!=,line session.beginTransaction(); Local operators and or not ETitle t1=new ETitle(); grouping Parenthesis( ),indicating t1.setPrice(500); t1.setWeburl("http://gmail.com"); in, not in, between, ist1.setTitleid(3); null is not null, is empty, is not empty, member of and not member t1.setTitle("Struts in Action"); of Title t2=new Title(); t2.setPrice(1550); t2.setTitleid(4); t2.setTitle("javaEE"); Simple case, case.when.then..ese..end, and searched case, case when t1.setWeburl("http://hotmail.com"); then..else.end Subject sub=new Subject(); String concatenation|| or concat(..,); sub.setSubcode("2"); sub.setSubname("javaEE"); Current_data(),current_time(),current_timestamp() sub.getTitles().add(t1); sub.getTitles().add(t2); Second(),minute(),hour(),day(),month(),year(.0,. t1.setSubject(sub); t2.setSubject(sub); session.save(t1); session.save(t2); Any function or operator defined by EJB-QL 3.0 substring(),trim(),lower(),upper(),length(), session.getTransaction().commit(); session.close(); Locate(),abs(),sqrt(),bit_length(),mod() } Coalesce(),and nullif() } Str() for converting numeric or temporal vaues to a readable String HQL query; function that take collectionvalued path expression: package minelement(),maxelement(),minindex(0,maxindex() import java.util.List; Any database-supported import java.util.Scanner; SQL scaler function like sign(),trunk(),rtrim(),sin(); JDBC-style positional parameters? import org.hibernate.Session; Java public static final constants eg.color.TABBY import org.hibernate.cfg.Configuration; size(),

avg(...), sum(...), min(...), max(...) public class HQlDemo {

public static void main(String[] args) throws Exception { Configuration cfg = new Configuration().configure(); count(*) Session session = cfg.buildSessionFactory().openSession(); session.beginTransaction(); count(...), count(distinct ...), count(all...) Scanner s = new Scanner(System.in); String query; while (true) { System.out.println("Enter the query"); query = s.nextLine(); if (query.length() == 0) break; try { List result = session.createQuery(query).list(); System.out.println("=================================== =============="); for (Object obj : result) { if (obj instanceof Object[]) { for (Object o : (Object[]) obj) { System.err.println(o.toString()); } } else { System.out.println(obj.toString()); } System.out.println(); } } catch (Exception e) { System.out.println("Error----------------------->"+e.getMessage()); } } } }

22

Query examples The following are HQL queries using Title,Etitle,and Subject entities.

From Title From Title where price>500 From Title where title likePro% From Title where instr(title,Asp)>0 Select t.title||-||t.price from Title t Select max(price)from Title Select t.subject.subname,max(t.price)from Title group by t,.subject.subname From Titlet.join t.subject s with s.subcode=java From Title t where t.price is not null From Title t where t.subject.subcoe in (java,C#);] From Title t where t.price=(select max(t.price) from Title t);

23
Query interface

An object-oriented representation of a Hibernate query. A Query instance is obtained by calling Session.createQuery(). This interface exposes some extra functionality beyond that provided by Session.iterate() and Session.find():

A particular page of the result set may be select by calling setMaxResults(),setFirstRetult() Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling You may not mix and match JDBC-style parameters and named parameters in the same query. Query are executed by caling list(),scroll(0or iterate(). A query may be re-executed by subsequent invocations. Its lifespan is , how ever bounded by the lifespan of the session that created. int executeUpdate() Execute the update or delete statement. List list() Return the query results as a List. String getQueryString() Get the query string. Iterator iterate() Return the query results as an Iterator. ScrollableResults scroll() Return the query results as ScrollableResults. Query Bind a value to a JDBC-style query parameter. setParameter(int position, Object val) Query setEntity(int position, Bind an instance of a mapped persistent class to a Object val) JDBC-style query parameter. Query Set a fetch size for the underlying JDBC query. setFetchSize(int fetchSize) Query Set the first row to retrieve. setFirstResult(int firstResult) Object uniqueResult() Convenience method to return a single instance that

matches the query, or null if the query returns no results.


Bulk updates Hibernate provides methods for bull SQL style DML statements execution which are performed through the Hibernate Query Language (UPDATE | DELETE ) [FROM] ENTITYnAME [(WHERE WHER_CONDITION)} There can only be a single entity named in the from clause; it can optionally be aliased. If the entity named is aliased, then any property references must be qualified using that alias. package query;

import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class BulkUpdates { public static void main(String[] args) throws Exception { // TODO code application logic here Configuration cfg = new Configuration().configure(); //cfg.configure("query/hibernate.cfg.xml"); //cfg.addResource("query/catalog.hbm/xml"); Session session = cfg.buildSessionFactory().openSession(); session.beginTransaction(); Transaction tx = session.getTransaction(); Query q = session.createQuery("update Title set price=price-50 where price<500"); int ct = q.executeUpdate(); System.out.println("No. rows updated: " + ct); tx.commit(); session.close(); } }

24

Criteria interface Criteria is a simplified API for retrieving entities by compositing criterion objects. This is a very convenient approach for functionality like search screen where there are variable number of conditions to be placed upon the result set. The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective. The interface org.hibernate.Criteria is used to create the criterion for the search. The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances. Here is a simple example of Hibernate Criterial Query:

package query; import java.util.Iterator; import java.util.List; import org.hibernate.*; public class CriteriaDemo { public static void main(String[] args)throws Exception { Configuration cfg = new Configuration().configure(); Session session = cfg.buildSessionFactory().openSession(); session.beginTransaction(); Criteria crit = session.createCriteria(Title.class); List insurances = crit.list(); for(Iterator it = insurances.iterator();it.hasNext();){ Title title = (Title) it.next(); System.out.println("ID: " +title.getTitleid() ); System.out.println("Name: " + title.getTitle()); } session.close(); } List result=session.createSQLQuery(select * from subjects).list(); } //print the result Query query=session.createSQLQuery("select * from titles where title like ?"); query.setParameter(0, "ja%"); NativeSQL List result =query.list(); You can also express queries in the native SQL dialect of your database. This is useful if you want for(Object obj:result){ to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. It also Title sub=(Title)obj; provides a clean migration path from a direct SQL/JDBC based application to Hibernate. System.out.println(sub.getTitle()); Hibernate3 allows you to specify handwritten SQL, including stored procedures, for all create, } update, delete, and load operations. List result=session.createSQLQuery("select * from titles"). addScalar("TITLEID",Hibernate.INTEGER). addScalar("Title",Hibernate.STRING).list(); for(Object obj:result){ ETitle sub=(ETitle)obj; System.out.println(sub.getTitle()); } ArrayList a = (ArrayList) session.createSQLQuery("select * from TITLES").addScalar("Title", Hibernate.STRING).list(); System.out.println(a);

25

Named query QBE allows you to obtained the result by providing sample data regarding the data you want. You populate an instance with the required data and then hibernate creates query with the dta provided by you in the instance. <query name="abc"> from Title </query> <sql-query name="discounts"> <return-scalar column="title" type="string"/> <return-scalar column="price" type="double"/> select * from titles </sql-query> List<Title> l = q.list(); for (Title e : l) { System.out.println("Titles " + e.getTitle()); } l=session.getNamedQuery("discounts").list();

Annotations in Hibernate

Introduction:- Hibernate needs a metadata to govern the transformation of data from POJO to database tables and vice versa. Most commonly XML file is used to write the metadata information in Hibernate. The Java 5 (Tiger) version has introduced a powerful way to provide the metadata to the JVM. The mechanism is known as Annotations. Annotation is the java class which is read through reflection mechanism during the runtime by JVM and does the processing accordingly. The Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code this helps the user to understand the table structure and POJO simultaneously during the development. This also reduces the management of different files for the metadata and java code. Prerequisite for setting the project :-

26
1. Make sure you have Java 5.0 or higher version . 2. You should have Hibernate Core 3.2.0GA and above. Let us assume we have a table Employee which has only two columns i.e ID and Name. In hibernate core to achieve the mapping for the above employee table the user should create the following files :1. Utility file for configuring and building the session factory. 2. Hibernate.cfg.xml or any other Datasource metadata file 3. Employee POJO object. 4. employee.hbm.xml file. (This file is now omitted for the ) 5. Real application file which has the actual logic manipulate the POJO Note:- Annotations are only the step to remove the hbm.xml file so all the steps remain same only some modifications in some part and adding the annotation data for the POJO.

package ann; import java.io.Serializable; import import import import import javax.persistence.Column; javax.persistence.Entity; javax.persistence.GeneratedValue; javax.persistence.Id; javax.persistence.Table;

@Entity @Table(name = "employee") public class Employee implements Serializable { public Employee() { } @Id @GeneratedValue @Column(name = "id") Integer id; @Column(name = "name") String name; @Column(name = "email") private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override`` public String toString() { return "Employee{" + "id=" + id + "name=" + name + "email=" + email + '}'; } }

27
package ann; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class EmployeeDemo { public static void main(String args[]){ Employee e=new Employee(); e.setId(1); e.setName("abc"); e.setEmail("abc@gmail.com"); AnnotationConfiguration ac=new AnnotationConfiguration(); ac.addAnnotatedClass(ann.Employee.class); SessionFactory sf=ac.configure().buildSessionFactory(); Session session=sf.openSession(); session.beginTransaction(); session.save(e); session.getTransaction().commit(); session.close(); sf.close(); } } attribute description default strategy the auto-generation strategy Amber should use AUTO generator sequence or table name ""

strategy AUTO IDENTITY SEQUENCE TABLE

description Choose type depending on database, e.g. IDENTITY for MySQL An auto-increment type of key generation, e.g. MySQL Use a sequence for key generation, e.g. Postgres or Oracle Use a separate Amber-controlled table for key generation

You might also like