Hibernate Framework Hibernate Associations and Relationships 2 — Questions and Answers
Question 1: Which annotation is used to map a one-to-one relationship between two entities in Hibernate?
- @OneToOne (Correct answer)
- @OneToMany
- @Singleton
- @UniqueRelation
Correct answer: @OneToOne
@OneToOne maps a single entity instance on one side to a single entity instance on the other side of the relationship.
Question 2: By default, how does Hibernate manage a unidirectional @OneToMany mapping without @JoinColumn?
- Using a foreign key on the child table
- Using an automatically created join table (Correct answer)
- Using a shared primary key
- Using an embedded composite key
Correct answer: Using an automatically created join table
Without @JoinColumn, Hibernate creates a separate join table by default to manage a unidirectional @OneToMany relationship.
Question 3: Which CascadeType causes a delete operation on a parent entity to propagate to its child entities?
- CascadeType.MERGE
- CascadeType.PERSIST
- CascadeType.REMOVE (Correct answer)
- CascadeType.REFRESH
Correct answer: CascadeType.REMOVE
CascadeType.REMOVE propagates the remove (delete) operation from the parent entity to all associated child entities.
Question 4: What is the default FetchType for a @ManyToOne relationship in Hibernate?
- FetchType.LAZY
- FetchType.EAGER (Correct answer)
- FetchType.SELECT
- FetchType.JOIN
Correct answer: FetchType.EAGER
@ManyToOne defaults to FetchType.EAGER, so the associated single entity is always loaded along with the owning entity.
Question 5: What is the purpose of the 'orphanRemoval = true' attribute in a @OneToMany annotation?
- Prevents null foreign key values in the child table
- Automatically deletes child entities removed from the parent's collection (Correct answer)
- Marks removed children as archived rather than deleted
- Prevents adding duplicate entries to the collection
Correct answer: Automatically deletes child entities removed from the parent's collection
When orphanRemoval is true, removing a child entity from the parent's collection automatically triggers a DELETE for that child in the database.
Question 6: In a @JoinTable annotation, which attribute specifies the foreign key columns referencing the owning entity's primary key?
- inverseJoinColumns
- joinColumns (Correct answer)
- referencedColumnName
- primaryKeyJoin
Correct answer: joinColumns
The 'joinColumns' attribute of @JoinTable defines the columns in the join table that reference the primary key of the owning (source) entity.
Question 7: How can two entities share the same primary key value in a @OneToOne relationship in Hibernate?
- Annotate both with @SharedPrimaryKey
- Use @MapsId on the dependent entity combined with @OneToOne (Correct answer)
- Use @PrimaryKeyJoinColumn on both sides
- Use @EmbeddedId on the parent entity
Correct answer: Use @MapsId on the dependent entity combined with @OneToOne
@MapsId on the dependent entity instructs Hibernate to use the parent entity's primary key as both the foreign key and primary key of the dependent entity.
Which annotation is used to map a one-to-one relationship between two entities in Hibernate?