@Entity, @Table, @Id — Concept Check
Questions on @Entity, @Table, @Id. @Entity marks a class as a JPA-managed entity; @Table customises the table name and schema; @Id designates the primary key field; every entity must have exactly one @Id.
Questions in this quiz
8 questions on @Entity, @Table, @Id — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to map a Java class to a database table with a different name using JPA. Which annotation should they use?
- A@Entity
- B@Table
- C@Id
- D@Column
2.A class annotated with @Entity must have which of the following?
- AMultiple @Id fields
- BStatic methods
- CA final class declaration
- DA no-arg constructor
3.A developer uses @IdClass for composite keys. What is a potential downside?
- ARequires no-arg constructor
- BCannot use field access
- CIncompatible with @Table
- DLess type safety compared to @EmbeddedId
4.A developer mistakenly believes that @Entity automatically customizes the table name. What is the correct understanding?
- A@Entity customizes the table name by default.
- B@Entity registers a class as a JPA entity; @Table customizes the table name.
- C@Entity and @Table both customize the table name.
- D@Entity is not required for JPA mapping.
5.What happens if a JPA entity is missing an @Id annotation?
- AThe entity will automatically generate a composite key.
- BThe entity will map successfully without issues.
- CThe application will fail at runtime due to missing primary key.
- DThe entity will use a default primary key.
6.A developer uses @EmbeddedId for a composite key. What is a key advantage of this approach?
- AAvoids using @Entity
- BType safety
- CSimpler JPQL queries
- DNo need for equals/hashCode
7.A developer wants to enforce a unique constraint on a column using JPA. Which annotation should they use?
- A@Column with unique
- B@Id
- C@Entity
- D@Table with uniqueConstraints
8.A developer believes that @Id is optional if @GeneratedValue is used. What is the correct understanding?
- A@Id is only needed for composite keys.
- B@GeneratedValue replaces the need for @Id.
- C@Id is optional if @GeneratedValue is present.
- D@Id is mandatory for all entities, even with @GeneratedValue.