Hibernate

What is polymorphism attribute in class element in Hibernate mapping document ?

It specifies whether to use implicit or explicit polymorphism. You can define mappings that specify a hierarchy of objects based on inheritance between Java classes.

When a query, usually a find(), is executed, Hibernate returns instances of this class when the class name or super class is named in the query; this is implicit polymorphism.

In explicit polymorphism, Hibernate returns instances of the class and its mapped sub-classes. This attribute is optional; the default is implicit.

What is persister attribute in class element in Hibernate mapping document ?

This attribute specifies the class that will be used to persist this particular class to the database.

What is optimistic-lock attribute in class element in Hibernate mapping document ?

This attribute specifies the type of locking used for the row, table, or database when updates need to occur in the database. The values available are:

none: No optimistic locking.
version: Checks the version or timestamp column.
dirty: Checks those columns that have been changed.
all: Checks all columns.

This attribute is optional; the default is version.

What is generator element in Hibernate ?

When Hibernate needs to add a new row to the database for a Java object that has been instantiated, it must fill the ID column with a unique value in order to uniquely identify this persisted object. Then this generator element comes into the picture.

What is increment generator in Hibernate ?

The increment generator is probably the most familiar creator of IDs. Each time the generator needs to generate an ID, it performs a select on the current database, determines the current largest ID value, and increment to the next value. If you are in a multithreaded environment this generator is not safe, because two or more threads could obtain the same ID value and cause an exception on the database server.

This generator supports all databases. It has no parameters. The possible column types are short, int, and long.

What is identity generator in Hibernate ?

If the database has an identity column associated with it, Hibernate can take advantage of the column to indicate when an object has not been added to the database. Supported databases include DB2, MySQL, Microsoft SQL Server, Sybase, and HypersonicSQL. It has no parameter. The possible column types are short, int, and long.

What is sequence generator in Hibernate ?

If the database has a sequence column associated with it, Hibernate can take advantage of the column to determine if the object has been added to the database. Supported databases include DB2, PostgreSQL, Oracle, SAP DB, McKoi, and InterBase. It has no parameter. The possible column types are short, int, and long.

How do you define sequence generated primary key in hibernate ?

Using <generator> tag.

Example:-

<id column="USER_ID" name="id" type="java.lang.Long">
   <generator class="sequence">
     <param name="table">SEQUENCE_NAME</param>
   <generator>
</id>

What is hilo generator in Hibernate ?

The hilo generator generates unique IDs for a database table. The IDs won’t necessarily be sequential. This generator must have access to a secondary table that Hibernate uses to determine a seed value for the generator. The default table is hibernate-unique-key, and the required column is next-value.

The possible column types are short, int, and long.

What is seqhilo generator in Hibernate ?

With the seqhilo generator, Hibernate combines the sequence and hilo generators. Supported databases include DB2, PostgreSQL, Oracle, SAP DB, McKoi, and InterBase. You can provide a sequence name with a value of hi_value or low_value depending on the sequence start. You can also provide a max_lo name with the starting value of the sequence. The possible column types are short, int, and long.

What is Uuid.hex generator in Hibernate ?

This generator creates a unique string based on appending the following values: the machine’s IP address, the startup time of the current JVM, the current time, and the counter value. It supports all databases and has no parameter. The possible column types are string, varchar, and text.

What is Uuid.string generator in Hibernate ?

This generator is like Uuid.hex, but the result is a string 16 characters long. It supports all databases except PostgreSQL, and it has no parameter. Possible column types are string, varchar, and text.

What is native generator in Hibernate ?

The native generator picks identity, sequence, or hilo, depending on the database.

What is assigned generator in Hibernate ?

If you need to assign the identifier yourself in your application, then use the assigned generator. You set the identifier using the set<identifier> method of the Java class. The application assumes the responsibility of making sure the id value is unique; otherwise, you will probably get a database insert exception.

This generator supports all databases. Some level of application validation may need to occur. It has no parameter. The column types are application dependent.

What is foreign generator in Hibernate ?

When a class is part of a one-to-one relationship, it can be helpful to have a common ID between objects. By specifying the foreign generator, you make the class use the ID of the other class. This generator supports all databases and has no parameter.

What is many-to-one relationship in Hibernate ?

In the course of object mapping, in some situations many objects associate to a single object. This is a many-to-one relationship; you use the <many-to-one> element for the mapping. The format of the element is as follows:

<many-to-one name="string" column="string" class="string"
    cascade=- [all] [none] [save-update] [delete]"
    outer-join="[true] [false] [auto]"
    update=" [true] [false]"
    insert="[true] [false]"
    property-ref="associated class" [optional]
    access=" [field] [property] [component class name]"
/>

What is one-to-one relationship in Hibernate ?

In the course of object mapping, in some situations an object is associated with a single object. This is a one-to-one relationship; you use the <one-to-one> element for the mapping. The format of the element is as follows:

<one-to-one name="name" class="class"
    cascade=" [all] [none] [save-update] [delete]"
    [optional - default none] constrained=" [true] [false]"
    outer-join=" [true] [false] [auto]"
    property-ref="property other than primary key of mapped table"
    [optional] access=" [field] [property] [component class name]"
/>

What is component element in Hibernate ?

The <component> element is designed to allow a property to be saved in the current object’s mapping from another class. The format of the element is:

<component name="name" class="class"
    update="true | false" [optional - defaults to true]
    insert="true | false" [optional - defaults to true]
    access="field | property | class" [optional - defaults to property]
    <property/>
    <many-to-one/>
/>

What is subclass element in Hibernate ?

When an inheritance relationship is defined for your Java classes, the hierarchy needs to be defined. Since Hibernate recommends the table-per-class-hierarchy mapping, you should define all sub-classes with the <subclass> element. Subclass definitions include attributes as well as the properties that have been added to this specific subclass. The format of the element is as follows:

<subclass name=”name”
    discriminator-value="value" [optional - defaults to name value]
    proxy="interface" [optional]
    lazy="true ( false" [optional]
    dynamic-update="true | false"[optional - defaults to false]
    dynamic-insert="true | false" [optional - defaults to false]
    <property/>
/>