Hibernate Framework Hibernate Mapping 1 — Questions and Answers
Question 1: Which annotation maps a Java class field to a specific database column?
- @Column (Correct answer)
- @Field
- @Attribute
- @DbColumn
Correct answer: @Column
The @Column annotation customizes the mapping between a Java field and its corresponding database column.
Question 2: What does the @Id annotation signify in a Hibernate entity?
- It marks the field as the primary key (Correct answer)
- It marks the field as indexed
- It marks the field as unique
- It marks the field as nullable
Correct answer: It marks the field as the primary key
The @Id annotation designates the field as the primary key of the entity's corresponding database table.
Question 3: Which @GeneratedValue strategy uses a database sequence?
- GenerationType.SEQUENCE (Correct answer)
- GenerationType.AUTO
- GenerationType.IDENTITY
- GenerationType.TABLE
Correct answer: GenerationType.SEQUENCE
GenerationType.SEQUENCE instructs Hibernate to use a database sequence object to generate primary key values.
Question 4: What is the difference between @OneToOne and @ManyToOne?
- @OneToOne maps a unique relationship while @ManyToOne maps multiple entities to one (Correct answer)
- @OneToOne is for primitives, @ManyToOne for objects
- They are identical
- @ManyToOne creates a join table
Correct answer: @OneToOne maps a unique relationship while @ManyToOne maps multiple entities to one
@OneToOne means one entity relates to exactly one other entity, while @ManyToOne means many entities can reference the same single entity.
Question 5: Which annotation marks a collection as a one-to-many relationship?
- @OneToMany (Correct answer)
- @CollectionOf
- @HasMany
- @ForeignKey
Correct answer: @OneToMany
The @OneToMany annotation maps a collection field where one entity owns many instances of another entity.
Question 6: What does the mappedBy attribute in @OneToMany indicate?
- The field on the owning side that holds the foreign key (Correct answer)
- The table name for the relationship
- The cascade type to apply
- The fetch strategy to use
Correct answer: The field on the owning side that holds the foreign key
The mappedBy attribute points to the field in the child entity that owns the foreign key for this bidirectional relationship.
Which annotation maps a Java class field to a specific database column?