Hibernate Framework Hibernate Mapping 2 — Questions and Answers
Question 1: Which annotation creates a join table for a many-to-many relationship?
- @JoinTable (Correct answer)
- @ManyToMany
- @RelationTable
- @CrossTable
Correct answer: @JoinTable
The @JoinTable annotation specifies the join table and its columns used to implement a many-to-many relationship.
Question 2: What does CascadeType.ALL mean in Hibernate?
- All persistence operations are cascaded to associated entities (Correct answer)
- Only delete operations cascade
- Cascade is disabled
- Only insert operations cascade
Correct answer: All persistence operations are cascaded to associated entities
CascadeType.ALL propagates all operations (persist, merge, remove, refresh, detach) from the parent entity to associated child entities.
Question 3: What does FetchType.LAZY mean in Hibernate?
- Associated data is loaded only when accessed (Correct answer)
- Associated data is loaded immediately with the parent
- Data is never loaded from the database
- Data is loaded asynchronously
Correct answer: Associated data is loaded only when accessed
FetchType.LAZY defers loading of associated entities until they are explicitly accessed in code.
Question 4: Which annotation is used to map an embedded value object in Hibernate?
- @Embedded (Correct answer)
- @ValueObject
- @Component
- @Inline
Correct answer: @Embedded
The @Embedded annotation maps a class whose fields are stored in the same table as the owning entity.
Question 5: What is the purpose of @Embeddable in Hibernate?
- Marks a class that can be embedded into another entity's table (Correct answer)
- Marks a class as a standalone entity
- Marks a class as a view
- Marks a class as transient
Correct answer: Marks a class that can be embedded into another entity's table
@Embeddable designates a class as one that can be embedded into an owning entity, sharing the same database row.
Question 6: Which annotation prevents a field from being persisted in Hibernate?
- @Transient (Correct answer)
- @Ignore
- @NotMapped
- @Exclude
Correct answer: @Transient
The @Transient annotation tells Hibernate to skip a field and not map it to any database column.
Which annotation creates a join table for a many-to-many relationship?