Home » Hibernate

Category Archives: Hibernate

Struts1 Spring and Hibernate Integration

Register.jsp

Failure.jsp

Success.jsp

web.xml

struts-config.xml

applicationContext.xml

RegisterForm.java

RegisterAction.java

Register.hbm.xml

Register.java

RegisterDao.java

RegisterDao.Impl.java

RegisterBoImpl.java

RegisterBo.java

Date range query on Timestamp field in HQL

Suppose we want to make query between two dates in a timestamp field for example
We have a table colors to store different colors

Pojo class for color table is

Above class shows that addDate is timestamp field and we want to query it as date for that we have casted timestamp field to date using cast() of hql. To fetching Color details with in date range we can use following code

Hibernate mapping files detail

Mapping Declaration

Object/relational mappings are usually defined in XML document. The mapping language is Java-centric, meaning that mappings are constructed around persistent class declarations, not table declarations.
Mapping Document

The element maps the domain object with corresponding entity in the database. hibernate-mapping element allows you to nest several persistent mappings, as shown above. It is however good practice to map only a single persistent class in one mapping file and name it after the persistent superclass, e.g. Employee.hbm.xml,

name (optional): The fully qualified Java class name of the persistent class (or interface). If this attribute is missing, it is assumed that the mapping is for a non-POJO entity.
table (optional – defaults to the unqualified class name): The name of its database table.
discriminator-value (optional – defaults to the class name): A value that distiguishes individual subclasses, used for polymorphic behaviour. Acceptable values include null and not null.
mutable (optional, defaults to true): Specifies that instances of the class are (not) mutable.
schema (optional): Override the schema name specified by the root <hibernate-mapping> element.
catalog (optional): Override the catalog name specified by the root <hibernate-mapping>  element.

<generator> element

The optional <generator> child element names a Java class used to generate 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 element.

All generators implement the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface; some applications may choose to provide their own specialized implementations. However, Hibernate provides a range of built-in implementations. There are shortcut names for the built-in generators:

increment
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster. .
identity
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequence
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
hilo
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.
seqhilo
uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid
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.
guid
uses a database-generated GUID string on MS SQL Server and MySQL.
native
picks identity, sequence or hilo depending upon the capabilities of the underlying database.
assigned
lets the application to assign an identifier to the object before save() is called. This is the default strategy if no element is specified.
select
retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value
foreign
uses the identifier of another associated object. Usually used in conjunction with a primary key association.

Detail of program

Hibernate client application is the actual application who wants to intrect with database software.
In the client application development two important roles are involved.

  1. get hibernate session object from a pool called hibernate seassionFactory.
  2. Use hibernate session object to perform curd operation on the database table.

Persistance operation

  1. single row operations,
  2. Bulk operations.

Single row operations.(use session object methods)

  1. sesion.save() insert.
  2. session.persist()        insert.
  3. Session.update()        update.
  4. session.delete()        delete.
  5. session.load()        select.

Bulk operation.

  1. using hql(hibernate query langauge)
  2. using criteria api         work as a classes and interface.
  3. Using Native sql query.

Hibernate Session Factory When hibernate client application calls configuration object hibernate software will be activated to read containent of hibernate configuration files property using DOM4j api leatter this properties value will be utilize to create a pool called Hibernate Session Factory

Hibernet first program

hibernate.cfg.xml

Employee.hbm.xml

Employee.java

EmpInsert.java

Whenever this program get compiled and run this application as simple java application run same this client application insert the records in database.

Directry Structure of hibernate

Directry Structure of hibernate is same as a simple java application

  1. We create application name
  2. Under the application we create package and lib folder
  3. Under of lib folder put all hibernate related jar files
  4. Under of packages put all class files and xml files

Components using hibernate based application

  1. Pojo class.
  2. Hbm.xml file.
  3. Hibernate configuration . xml file.
  4. Client class of hibernate.

Pojo class It is an ordinary java class having member variable.the pojo class will be mapped with table and member variable will be mapped with table columns in the process of o-r mapping.this class will used by hibernate client application to developed database independence persistence logic.
Hibernate mapping file(hbm.xml) can becomes hibernate mapping file.but it should be specified as the property value of hibernate this file contains xml enteries to perform o-r mapping and also to specify object level releationship.
Hibernate configuration file In earlier version it was a properties file from hibernate 2.2x it is change if no file is explicitly specified it becomes a hibernate-cfg.xml file as the hibernate configuration file this contain following minimum information

  1. jdbc driver class name.
  2. jdbc url.
  3. database username.
  4. database password.
  5. dilect entry point out database.
  6. name of the mapping file.

Hibernate configuration file information needed to connect to persistent layer and the linked mapping documents. You can either specify the data source name or JDBC details that are required for hibernate to make JDBC connection to the database. The element refers to the mapping document that contains mapping for domain object and hibernate mapping document

Hibernate client application.this can be any java/j2ee components or application who actually wants to develop database independent persistence logic using pojo class and hql.this uses core api of hibernate to activate hibernate software and to load hibernate configuratiobn file and pojo class object from the memory.
In configuration file based approach, “hibernate.cfg.xml” is placed in the classpath, Following Hibernate code can be used in this method.

Hibernate Architecture

Hibernate support all layer architecture single layer,2 layer,n layer
High level architecture of Hibernate can be described as shown in following illustration.

Hibernate configuration file
In earlier version it was a properties file from hibernate 2.2x it is change if no file is explicitly specified it becomes a hibernate-cfg.xml file as the hibernate configuration file this contain following minimum information

  1. jdbc driver class name.
  2. jdbc url.
  3. database username.
  4. database password.
  5. dilect entry point out database.
  6. name of the mapping file.

Introduction to Hibernate

Hibernate is an open source free software of object/relational mapping tool for Java. Hibernate lets you develop persistent classes following common Java idiom – including association, inheritance, polymorphism, composition and the Java collections framework.

Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.

Features of Hibernate

Transparent persistence without byte code processing

Object-oriented query language
Powerful object-oriented query language
Full support for polymorphic queries
New Criteria queries
Native SQL queries

Object / Relational mappings
Three different O/R mapping strategies
Multiple-objects to single-row mapping
Polymorphic associations
Bidirectional associations

Automatic primary key generation
Multiple synthetic key generation strategies
Support for application assigned identifiers
Support for composite keys

Object/Relational mapping definition
XML mapping documents
Human-readable format
XDoclet support

HDLCA (Hibernate Dual-Layer Cache Architecture)
Thread safeness
Non-blocking data access
Session level cache
Optional second-level cache
Optional query cache
Works well with others

High performance
Lazy initialization
Outer join fetching
Batch fetching
(Optional) Internal connection pooling and PreparedStatement caching

J2EE integration
JMX support
Integration with J2EE architecture (optional)
New JCA support

Managed by MochaHost - Fast & Easy WordPress Hosting