Sample questions
Question 1Easy
What does HQL/JPQL query against?
- Database table and column names only
- Entity classes and their properties
- Servlet request objects
- Raw files on disk
Answer: B. Entity classes and their properties
Explanation
HQL and JPQL are object oriented: from User u where u.age > 18 uses class and field names, and the provider translates to SQL.
Step-by-step solution
- You write the query with entity names.
- The provider converts it to SQL for the dialect.
- Results come back as entities.
Why the other options are wrong
- A. Native SQL uses table names; HQL does not.
- C. Requests have nothing to do with queries.
- D. Files on disk are not queried.
Question 2Easy
What must a JPA entity class have to satisfy the specification?
- A final modifier and a static id
- Only getters and setters
- A main method
- A no-arg constructor and an @Id field
Answer: D. A no-arg constructor and an @Id field
Explanation
JPA needs an @Entity class with a public or protected no-arg constructor and an @Id identifier.
Step-by-step solution
- Annotate the class @Entity.
- Mark the primary key with @Id.
- Provide a no-arg constructor.
Why the other options are wrong
- A. Entities must not be final since proxies subclass them.
- B. The id must not be static.
- C. Accessors are common but not the required pair here.
Question 3Easy
In Hibernate/JPA, which annotation marks a class as a persistent entity?
- @Table only
- @Entity
- @Component
- @Embeddable
Answer: B. @Entity
Explanation
@Entity tells the provider that instances map to rows. @Table merely customises the table name and is optional.
Step-by-step solution
- Annotate the class with
@Entity. - Add one field with
@Id. - The provider maps the class to a table.
Why the other options are wrong
- A.
@Table alone does not register an entity. - C.
@Component is a Spring stereotype. - D.
@Embeddable marks a value type embedded into another entity.
Question 4Medium
Which entity state describes an object created with new that is not associated with any session?
- Persistent
- Detached
- Removed
- Transient
Answer: D. Transient
Explanation
A new object with no identity in the database and no session is transient. persist makes it persistent.
Step-by-step solution
- new Employee() creates a transient object.
- persist attaches it.
- Closing the session detaches it.
Why the other options are wrong
- A. Persistent means attached to a session.
- B. Detached was once persistent but its session closed.
- C. Removed is scheduled for deletion.
Question 5Medium
An entity object that was saved and then its Session was closed is in which state?
- Detached
- Transient
- Persistent
- Removed
Answer: A. Detached
Explanation
Transient objects were never saved. Persistent ones are attached to an open session. Once the session closes the object still has an id but is no longer tracked: detached.
Step-by-step solution
new User() is transient.persist makes it persistent.session.close() makes it detached.
Why the other options are wrong
- B. Transient means never associated with a session.
- C. Persistent needs an open session.
- D. Removed means scheduled for deletion.
Question 6Medium
In @OneToMany(mappedBy = "dept"), what does mappedBy tell JPA?
- This side owns the foreign key column
- The other side owns the foreign key
- The relation is many-to-many
- The collection must be eager
Answer: B. The other side owns the foreign key
Explanation
mappedBy names the field on the owning entity, which carries the join column. This side is the inverse.
Step-by-step solution
- Employee has @ManyToOne dept.
- Department has @OneToMany(mappedBy="dept").
- Foreign key sits in employee table.
Why the other options are wrong
- A. The owning side is the one without mappedBy.
- C. It is a one-to-many, not many-to-many.
- D. Fetch type is set separately.
Question 7Medium
What does dirty checking do in a Hibernate transaction?
- Cleans up unused sessions after the timeout period
- Deletes unreferenced rows from the database automatically
- Detects changes to persistent entities and writes updates at flush time without calling update()
- Validates the schema against the mapping files at startup
Answer: C. Detects changes to persistent entities and writes updates at flush time without calling update()
Explanation
Hibernate snapshots loaded entities. At flush it compares and issues UPDATE for changed ones automatically.
Step-by-step solution
- Load entity.
- Change a field via setter.
- Commit; Hibernate emits UPDATE.
Why the other options are wrong
- A. It is not about sessions cleanup.
- B. No deletions happen without a remove.
- D. Schema validation is a separate setting.
Question 8Medium
What is the difference between session.get(User.class, 5) and session.load(User.class, 5) in classic Hibernate when no row with id 5 exists?
- get throws immediately; load returns null
- Both return null whenever the row is missing
- get returns null; load returns a proxy that throws an exception when you use it
- Both return a new empty User object with default field values
Answer: C. get returns null; load returns a proxy that throws an exception when you use it
Explanation
get hits the database straight away and returns null for a missing row. load returns a lazy proxy and the not-found exception appears only on first access.
Step-by-step solution
get queries immediately.- Missing row gives null.
load gives a proxy; first use triggers the query and failure.
Why the other options are wrong
- A. The behaviours are swapped in this option.
- B. Only
get returns null. - D. Hibernate never invents an empty entity.
Question 9Hard
With @OneToMany(cascade = CascadeType.REMOVE), what happens when the parent is removed?
- Only the parent is removed and children are orphaned silently
- Its child entities are removed too
- Children are detached
- The operation is blocked
Answer: B. Its child entities are removed too
Explanation
Cascade REMOVE propagates remove to associated entities, so the child rows are deleted with the parent.
Step-by-step solution
- Call em.remove(parent).
- Cascade applies remove to children.
- Rows deleted in order.
Why the other options are wrong
- A. REMOVE cascades to children.
- C. Children are also deleted, not kept.
- D. Cascade does not block.
Question 10Hard
What is the N+1 select problem?
- One query for N parents, then one more per parent for a lazy association
- A table design that has N+1 columns and slows queries
- N queries run in parallel and overload the database server
- The cache stores N+1 copies of each row
Answer: A. One query for N parents, then one more per parent for a lazy association
Explanation
Iterating parents and touching a lazy child collection triggers a query each time. A join fetch or batch fetching reduces it.
Step-by-step solution
- Run 1 query for parents.
- Each parent's lazy child loads separately.
- Total is N+1 queries.
Why the other options are wrong
- B. It is about query count, not columns.
- C. It is sequential extra queries, not parallelism.
- D. It is not a caching duplication problem.
Tip: Use JOIN FETCH.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →