Interview :: Hibernate
There are 3 states of the object (instance) in hibernate.
- Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
- Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
- Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.
What are the inheritance mapping strategies?
There are 3 ways of inheritance mapping in hibernate.
- Table per hierarchy
- Table per concrete class
- Table per subclass
If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is mutable="true".
The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.
Let's understand it by the example given below:
Here, after getting employee instance e1 and we are changing the state of e1.
After changing the state, we are committing the transaction. In such a case, the state will be updated automatically. This is known as dirty checking in hibernate.
There can be 4 types of association mapping in hibernate.
- One to One
- One to Many
- Many to One
- Many to Many
No, collection mapping can only be performed with One-to-Many and Many-to-Many.
Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, and you don't need to do lazy="true". It means not to load the child objects when the parent is loaded.
Hibernate Query Language is known as an object-oriented query language. It is like a structured query language (SQL).
The main advantage of HQL over SQL is:
- You don't need to learn SQL
- Database independent
- Simple to write a query
No. | First Level Cache | Second Level Cache |
---|---|---|
1) | First Level Cache is associated with Session. | Second Level Cache is associated with SessionFactory. |
2) | It is enabled by default. | It is not enabled by default. |