Hibernate Interview Questions – Part 2

Hibernate Interview Questions – Part 2
(Please check here for Hibernate Interview Questions Part -1)

How can you map Beans with Database?
Using XML
Using Annotations


Explain Mapping with XML File:
<DOCTYPE>:
Specifies the type of XML and the name of Document Type Definition(DTD) contains rule to validate elements

<hibernate-mapping>:
Root of mapping elements

<class>:
Refers to Java class mapped with database
Attributes:
name
– class name
table
– name of database table to which class to be mapped
mutable – If objects in java class are read-only

<id>:
Primary key column of database.
Attributes:
name
– property of java class mapped to primary key column of database
type – hibernate type used to map property of java class with primary key column of database
column – specifies primary key column of database.

<generator>:
Generates unique identifiers.
Types:
assigned:
Lets the application assign an identifier to the object before it gets saved in database
increment:
Generates unique identifiers for objects stored in database (int, long, short types)
identity:
Automatic generation of values in primary key columns. (supports various databases like db2, oracle etc)
sequence:
Automatic generation of unique identifiers in a sequence
native:
Automatic generation of unique identifiers using identity or sequence depending on database

<property>:
Maps properties of Java class with database columns
Attributes:
name – specifies name of property in java class to map column in database
type –  specifies type of property in java class to map column in database
column – name of the column in database
length – length of characters for a column

<column>:
Specifies the column that is to be mapped with database
Attributes:
name – name of the column in database
length – length of characters for that column


Explain mapping using Annotations:
@Entity
– specify the java class as entity bean

@Table – name of the table

@Column – name of the column
name – name of the column
length – length of the column
unique – contains unique values
nullable – column cannot be null

@Id – primary key

@GeneratedValue – primary key generation strategy
AUTO – Generates primary for each new entity object.
IDENTITY – Similar to AUTO except it is unique only per type Hierarchy
SEQUENCE – Generates ID using defined sequence number(maintains the next sequence number to be used)
TABLE – Similar to sequence, except this stores the last sequence number that’s been used.


Explain Mapping Collections:
Commonly used Mapping collections are List, Set and Bag.
List – Contains duplicate items and implicit ordering of elements
Set – does not contain duplicate items
Bag – Contains duplicates but is not indexed


What is Cascade?
cascade Attribute:
Specifies operations performed in an entity and related entities too.

none: Changes made only in that entity and not to related entities
save-update: Save and Update changes are done in specific entity and all related entities too.
delete:  Delete operation performed in specific entity and related entities.
all: Changes made by save, delete, update affects all the related entities too


 

3 thoughts on “Hibernate Interview Questions – Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *