Hibernate tutorial download free




















What is ORM? Advantages 1 Lets business code access objects rather than DB tables. An ORM solution consists of the following four entities: S. A language or API to specify queries that refer to classes and properties of 2 classes. A technique to interact with transactional objects to perform dirty checking, 4 lazy association fetching, and other optimization functions.

A persistent framework is an ORM service that stores and retrieves objects into a relational database. Following is list of few of the database engines supported by Hibernate. Hibernate makes use of the database and configuration data to provide persistence services and persistent objects to the application.

Following is a very high level view of the Hibernate Application Architecture. Following is a detailed view of the Hibernate Application Architecture with few important core. JDBC provides a rudimentary level of abstraction of functionality common to relational databases, allowing almost any database with a JDBC driver to be supported by Hibernate.

Following section gives brief description of each of the class objects involved in Hibernate Application Architecture. Configuration Object: The Configuration object is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization.

It represents a configuration or properties file required by the Hibernate. These files are hibernate. SessionFactory Object: Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated.

The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is heavyweight object so usually it is created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.

So if you are using multiple databases then you would have to create multiple SessionFactory objects. Session Object: A Session is used to get a physical connection with a database.

The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object. The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed. This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.

A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query. Criteria Object: Criteria object are used to create and execute object oriented criteria queries to retrieve objects. Hibernate Environment Setup This chapter will explain how to install Hibernate and other associated packages to prepare a develop environment for the Hibernate applications. Downloading Hibernate: It is assumed that you already have latest version of Java is installed on your machine.

Following are the simple steps to download and install Hibernate on your machine. Final and when you unzip the downloaded file it will give you directory structure as follows.

Installing Hibernate: Once you downloaded and unzipped the latest version of the Hibernate Installation file, you need to perform following two simple steps. This file lies in the root directory of the installation and is the primary JAR that Hibernate needs to do its work. Hibernate also requires a set of configuration settings related to database and other related parameters.

All such information is usually supplied as a standard Java properties file called hibernate. I will consider XML formatted file hibernate. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application's classpath. Hibernate Properties: Following is the list of important properties you would require to configure for a databases in a standalone situation: S.

Properties and Description hibernate. If you are using a database along with an application server and JNDI then you would have to configure the following properties: S. Let us createhibernate. You would have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database. Following is the list of various important databases dialect property type: Database Dialect Property DB2 org.

InformixDialect Ingres org. OracleDialect Oracle 11g org. Oracle10gDialect Oracle 10g org. Oracle10gDialect Oracle 9i org. SybaseDialect Sybase Anywhere org. The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes.

A persistent instance has a representation in the database, an identifier value and is associated with a Session. A Session instance is serializable if its persistent classes are serializable.

Session Interface Methods: There are number of methods provided by the Session interface but I'm going to list down few important methods only, which we will use in this tutorial. You can check Hibernate documentation for a complete list of methods associated with Session and SessionFactory.

Session Methods and Description Transaction beginTransaction 1 Begin a unit of work and return the associated Transaction object. Criteria createCriteria Class persistentClass 5 Create a new Criteria instance, for the given entity class, or a superclass of an entity class.

Criteria createCriteria String entityName 6 Create a new Criteria instance, for the given entity name. Serializable getIdentifier Object object 7 Return the identifier value of the given entity as associated with this session. Query createFilter Object collection, String queryString 8 Create a new instance of Query for the given collection and filter string.

Session get String entityName, Serializable id 13 Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance. SessionFactory getSessionFactory 14 Get the session factory which created this session. Transaction getTransaction 16 Get the Transaction instance associated with this session. Serializable save Object object 20 Persist the given transient instance, first assigning a generated identifier.

Hibernate Persistent Class The entire concept of Hibernate is to take the values from Java class attributes and persist them to a database table. A mapping document helps Hibernate in determining how to pull the values from the classes and map them with table and associated fields.

Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. There are following main rules of persistent classes, however, none of these rules are hard requirements. This property maps to the primary key column of a database table. This mapping file instructs Hibernate how to map the defined class or classes to the database tables.

Though many Hibernate users choose to write the XML by hand, a number of tools exist to generate the mapping document. Let us consider our previously defined POJO class whose objects will persist in the table defined in next section.

We saved our mapping document in the file Employee. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table.

The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. Set the class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table.

There are other attributes and elements available which will be used in a mapping document and I would try to cover as many as possible while discussing other Hibernate related topics. The types declared and used in the mapping files are not Java data types; they are not SQL database types either.

These types are called Hibernate mapping types, which can translate from Java to SQL data types and vice versa. This chapter lists down all the basic, date and time, large object, and various other builtin mapping types. String CHAR 1 string java.

Date or java. Date DATE time java. Time TIME timestamp java. Serializable clob java. Clob CLOB blob java. We will go through different steps involved in creating Java Application using Hibernate technology.

When you design a classs to be persisted by Hibernate, it's important to provide JavaBeans compliant code as well as one attribute which would work as index like id attribute in the Employee class.

There would be one table corresponding to each object you are willing to provide persistence. Create Application Class: Finally, we will create our application class with the main method to run the application.

We will use this application to save few Employee's records and then we will apply CRUD operations on those records. List; import java. Date; import java. Iterator; import org. HibernateException; import org. Session; import org. Transaction; import org. SessionFactory; import org. These are the mapping of collections, the mapping of associations between entity classes and Component Mappings.

Collections Mappings: If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java. Hibernate can persist instances ofjava. Map, java. Set, java. SortedMap, java. SortedSet, java. List, and any array of persistent entities or values.

Set java. SortedSet java. The sort attribute can be set to either a comparator or natural ordering. List java. Collection with java. Map java. SortedMap java. However, they are rarely used so I'm not going to discuss them in this tutorial.

If you want to map a user defined collection interfaces which is not directly supported by Hibernate, you need to tell Hibernate about the semantics of your custom collections which is not very easy and not recommend to be used.

Association Mappings: The mapping of associations between entity classes and the relationships between tables is the soul of ORM. Following are the four ways in which the cardinality of the relationship between the objects can be expressed. An association mapping can be unidirectional as well as bidirectional. Mapping type Description Many-to-One Mapping many-to-one relationship using Hibernate One-to-One Mapping one-to-one relationship using Hibernate One-to-Many Mapping one-to-many relationship using Hibernate Many-to-Many Mapping many-to-many relationship using Hibernate Component Mappings: It is very much possible that an Entity class can have a reference to another class as a member variable.

If the referred class does not have it's own life cycle and completely depends on the life cycle of the owning entity class, then the referred class hence therefore is called as the Component class. It is an open source persistent framework created by Gavin King in Hibernate 3 in action pdf free download An eBook of the previous edition is included at no additional cost when you buy the revised edition!

Hibernate in Action carefully explains the concepts you need, then gets you going. It builds on a single about Hibernate3 and EJB 3.

Download free training document material in PDF for developer who wants to learn the basics of Hibernate. Take advantage of this course called Java Persistence and Hibernate Guide for developer to improve your Programming skills and better understand Hibernate.



0コメント

  • 1000 / 1000