What is Hibernate Query Language (HQL) ?

Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.

How do you map Java Objects with Database tables ?

First we need to write Java domain objects (beans with setter and getter).
Write hbm.xml, where we map java class to table and database columns to Java class variables.

Example :

<hibernate-mapping>
    <class name="com.test.User"  table="user">
        <property  column="USER_NAME" length="30"
            name="userName" not-null="true"  type="java.lang.String"/>
        <property  column="USER_PASSWORD" length="255"
            name="userPassword" not-null="true"  type="java.lang.String"/>
    </class>
</hibernate-mapping>

What are the difference between load() and get() ?

Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get() methods.

load() method will throw an exception if the unique id is not found in the database. get() method will return null if the unique id is not found in the database.

load() just returns a proxy by default and database won’t be hit until the proxy is first invoked. get() will hit the database immediately.

What is the difference between and merge and update ?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

What are two important subelements of class element in Hibernate ?

The <class> element includes numerous elements to fully map the Java class, but two important subelements: <id> and <property>.
The <id> element maps the unique ID attribute added to our CD class to the primary key of the database.
The <property> element is name, which is the name of the attributes in our CD class. Hibernate uses reflection to locate the setter/getter methods for each attribute specified. Any attributes that we don’t want to be persisted aren’t listed.

What are attributes of <id> element in Hibernate ?

name: The attribute name found in the CD.
type: The class type given to the ID attribute.
unsaved-value: Specifies whether Hibernate should persist an object. The identifier attribute of a class is called id; it holds a value of null for a new object and an integer value for an object pulled from storage. Hibernate obtains the value of the identifier and compares it to the value specified in the unsaved-value attribute to determine whether Hibernate should persist the object. If the default value of the identifier used in your application isn’t null, you need to place the default value in the unsaved-value attribute.

What are subelements of <id> element in Hibernate ?

The <id> element also includes a couple of subelements: <column> and <generator>.

The <column> element tells Hibernate what specific column in the mapped database table relates to the identifier for the class.
The <generator> element specifies the generator to be used when creating the unique identifier. The generator class is specified in the name attribute of the <generator> element.

How to obtain SessionFactory in Hibernate ?

The Configuration object handles all the parsing and internal processing of mapping documents. Most of the real work done by Hibernate occurs in a Session object. To build a Session object, a SessionFactory object needs to be instantiated from the Configuration object. The buildSessionFactory() method builds a factory based on the specifics of the mapping documents processed by the Configuration object. Once the SessionFactory object has been created, the Configuration object can be discarded.

What are Hibernate JDBC properties ?

hibernate.connection.driverclass

you must specify JDBC driver class with this property. You can find a listing of available JDBC drivers at http://servlet.java.sun.com/products/jdbc/drivers/ index.html.

Example: com.mysgl.jdbc.Driver

hibernate.connection.url

The URL property determines where the database server is located and how to connect with it. The URL has this
format: <protocol>:<subprotocol>:<subname>

The format of the <subname> is: //<host>[: <port>] [/<database>]

Example: jdbc:mysgl://192.168.1.21/products

hibernate.connection.username

Each application that attaches to a database should have a unique username and password associated with it. You can specify the username using this property. A unique username is important because in most database systems, you can specify the permissions available to that username; doing so adds a level of protection when you’re running multiple databases on a single system.

hibernate.connection.password

This property specifies the password to be used with the corresponding username.

hibernate.connection.pool_size

You can use this property to specify the total number of connections to initially establish with the database server.

hibernate.connection.datasource

If you’re working with a JNDI connection, the datasource string is specified in this property.

hibernate.jndi.url

The URL, as defined earlier, is specified in this property when a JNDI source is being used and a URL isn’t specified with the datasource. This property is optional.

hibernate.jndi.class

This property specifies the name of the JNDI class that implements InitialContextFactory. This property is optional.

hibernate.dialect

This property specifies the name of the dialect to use for the database connection.

hibernate.default_schema

In SQL query strings, you provide the table or tables from which information should be pulled without specifying schema or tablespaces. If you want your table to be fully qualified, use the default_schema property to specify the schema or tablespace Hibernate can generate with its SQL calls to the database server.

hibernate.Session_factory_name

If you’re using JNDI and wish to provide a binding between the SessionFactory class and a JNDI namespace, use the session factory_name property. Provide the name, and Hibernate uses the values specified in hibernate.jndi and hibernate.jndi.class to create the initial context.

hibernate.use_outer_join

In SQL, an outer join can provide a performance increase because fewer database accesses are needed. By default, the use_outer_join property is true. If you don’t want to allow outer joins, set the property to false. For each relationship in an application’s set of tables, the outer join attribute determines the correct action.

hibernate.max_fetch_depth

The database server can create an outer join using a graph, where the nodes in the graph are tables and the edge and relationships are join conditions. Using the graph, a depth-first search can be performed to determine which tables will be part of the outer join. You can limit the depth of the search using this property. Note that outer joins may reduce the communication with the database, but a lot of work goes into building the outer join SQL string correctly.

hibernate.jdbc.fetch_size

When JDBC performs a query against the database, some rows are returned based on the fetch size for the JDBC driver being used. You can use this property to set the total number of rows retrieved by each JDBC fetch from the database.

hibernate.jdbc.batch_size

The JDBC driver groups some number of INSERT, UPDATE, DELETE commands together and sends them as a batch to the server, thus reducing the communication time between the client and the database. If the batch_size property is set to a nonzero value, Hibernate attempts to use batch updates. The value used in the batch_size property dictates the total number of updates to use per batch.

hibernate.jdbc.use_scrollable_resultset

If you’re using a user-supplied connection and wish to have Hibernate use scrollable resultsets, set this property to true. If you’re using a Hibernate-created connection to the database server, this property has no effect.

hibernate.jdbc.use_streams_for_binary

Instead of directly copying the binary data to the query string, JDBC lets you use a stream. To make JDBC use streams for binary data, set this property to true.

hibernate.cglib.use_reflection_optimizer

By default, Hibernate uses CGLIB; but you can defeat this behavior by setting this property to false. If the property has a false value, Hibernate uses runtime reflection instead.

hibernate.jndi.<property>

You can provide extraneous properties for JNDI’s InitialContextFactory class using this property. The property and its value are passed to the JNDI factory.

hibernate.connection.isolation

When a database is confronted with multiple clients making updates and queries (a situation called database concurrency), data may be changed and returned to clients with wrong values. There are three primary concurrency issues: Dirty Read, Nonrepeatable Read and Phantom Insert. You can solve all three of these situations using the isolation levels associated with a database and ANSI SQL.

hibernate.connection.provider_class

If you’re using a JDBC driver that includes a custom ConnectionProvider class, you need to specify that class in this property.

hibernate.transaction.factory_class

This property names a specific TransactionFactory class to be used with Hibernate.

jta.UserTransaction

This property specifies the JNDI name for the JTATransactionFactory.

hibernate.show_sql

If you want a log of the SQL commands generated by Hibernate to the underlying database, set the show_sql property to true.

hibernate.query.substitutions

When you’re using the Hibernate Query Language (HQL), Hibernate formulates appropriate SQL statements according to the dialect specified for the database. If you need to help Hibernate with a query, you can specify substitutions in the Hibernate configuration file.