Hibernate Interview Questions – Part 2
(Please check here for Hibernate Interview Questions Part -1)
How can you map Beans with Database?
Explain Mapping with XML File:
Specifies the type of XML and the name of Document Type Definition(DTD) contains rule to validate elements
Root of mapping elements
Refers to Java class mapped with database
name – class name
table – name of database table to which class to be mapped
mutable – If objects in java class are read-only
Primary key column of database.
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.
Generates unique identifiers.
Lets the application assign an identifier to the object before it gets saved in database
Generates unique identifiers for objects stored in database (int, long, short types)
Automatic generation of values in primary key columns. (supports various databases like db2, oracle etc)
Automatic generation of unique identifiers in a sequence
Automatic generation of unique identifiers using identity or sequence depending on database
Maps properties of Java class with database columns
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
Specifies the column that is to be mapped with database
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?
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