What does hibernate.hbm2ddl.auto create mean? (a) Create session object automatically (b) Create Sessio Factory object automatically (c) Create tables automatically (d) None of the above Is Hibernate Session threadsafe? (a) No (c) No relation with thread (b) Yes (d) None of the above Which of the following is not a method on Session interface? (a) save() (c) saveorupdate() (b) remove() (d) load()
The correct answer and explanation is :
Question 1: What does hibernate.hbm2ddl.auto=create mean?
Correct Answer: (c) Create tables automatically
Explanation:
The hibernate.hbm2ddl.auto property in Hibernate is used to control how Hibernate handles the schema (database structure) generation and validation. The possible values for this property are:
- create: This means that Hibernate will drop the existing database schema and create a new schema based on the mappings specified in your entity classes. It is typically used in development environments to automatically create tables for the entities.
- update: Hibernate will attempt to update the schema based on the entity mappings without dropping it. It is useful in situations where the schema is evolving.
- validate: Hibernate validates the existing schema with the mappings in the code. It doesn’t modify the schema but will throw an exception if there is any mismatch between the schema and the mappings.
- none: No action will be taken regarding the schema, meaning it won’t create, update, or validate anything.
So, when hibernate.hbm2ddl.auto is set to create, it means Hibernate will automatically create the tables in the database based on the entity mappings.
Question 2: Is Hibernate Session thread-safe?
Correct Answer: (a) No
Explanation:
A Hibernate Session is not thread-safe. A Hibernate Session represents a single unit of work and is meant to be used by one thread at a time. It manages a set of entities and their lifecycle states during a transaction. Since a session is tied to a particular transaction, concurrent access by multiple threads to the same session could lead to unexpected behavior or errors.
To avoid issues, Hibernate recommends using a SessionFactory to create session instances, and each thread should maintain its own session. Typically, the session is bound to a single thread and should be closed after the transaction is complete.
To manage thread safety:
- SessionFactory: It is thread-safe and typically shared across multiple threads. It is used to create
Sessionobjects. - Session: Not thread-safe and should not be shared between threads. Each thread should have its own instance of
Session.
Question 3: Which of the following is not a method on the Session interface?
Correct Answer: (b) remove()
Explanation:
The Hibernate Session interface provides various methods to manage entities. Here’s a breakdown of the methods listed:
- save(): Persists an entity instance to the database, assigning it a primary key.
- saveOrUpdate(): Saves an entity if it is new, or updates it if it already exists (based on the primary key).
- load(): Retrieves an entity by its identifier, but the entity is lazily loaded (not fully fetched until it is accessed).
However, remove() is not a method on the Session interface. The correct method to delete an entity is delete(), which removes an entity from the database.
Summary:
- hibernate.hbm2ddl.auto=create means that Hibernate will create tables automatically.
- A Hibernate Session is not thread-safe, so each thread should maintain its own session.
- The remove() method is not part of the
Sessioninterface; the correct method for deleting an entity is delete().
These details are essential for understanding how Hibernate interacts with databases, manages sessions, and ensures thread safety.