Given the following JPA entities, what will be printed when fetching a Book entity and accessing its author field?
import jakarta.persistence.*; @Entity public class Author { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getters and setters } @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @ManyToOne @JoinColumn(name = "author_id") private Author author; // getters and setters } // In a service method: Book book = entityManager.find(Book.class, 1L); System.out.println(book.getAuthor().getName());
Consider the default fetch type of @ManyToOne and what happens when you access the related entity.
The default fetch type for @ManyToOne is EAGER, so the related Author entity is fetched automatically when the Book is loaded. Therefore, accessing book.getAuthor().getName() prints the author's name.
Choose the correct code snippet that defines a @ManyToOne relationship with a join column named category_id in a Product entity.
Remember the correct attribute name for specifying the column name in @JoinColumn.
The correct attribute to specify the join column name is name. Option A uses @JoinColumn(name = "category_id") which is correct. Option A misses the attribute name, C uses a wrong attribute columnName, and D misses quotes around the string.
Consider this code snippet:
Book book = entityManager.find(Book.class, 1L); entityManager.close(); System.out.println(book.getAuthor().getName());
Why does accessing book.getAuthor().getName() throw a LazyInitializationException?
Check the default fetch type of @ManyToOne and when the related entity is loaded.
By default, @ManyToOne fetch type is EAGER, but if it is explicitly set to LAZY, the related entity is not loaded until accessed. If the entity manager is closed before accessing the lazy field, a LazyInitializationException occurs because the proxy cannot load the data.
Given these entities:
@Entity
class Order {
@Id
Long id;
@ManyToOne
Customer customer;
}
@Entity
class Customer {
@Id
Long id;
String name;
}And this code:
Order order = new Order();
Customer customer = new Customer();
customer.setName("Alice");
order.setCustomer(customer);
entityManager.persist(customer);
entityManager.persist(order);
entityManager.flush();What is true about the database state after flush?
Think about the order of persisting and how JPA handles relationships.
Both entities are persisted explicitly. The Customer gets an id when persisted. The Order references the Customer, so the foreign key column customer_id in Order points to the Customer's id. The flush writes both to the database.
Consider a @ManyToOne relationship from Invoice to Client. Which of these statements about cascading operations is true?
Think about the direction of cascading and what happens when you delete the owning entity.
Cascading from the many side (@ManyToOne) to the one side means operations on the child entity (Invoice) propagate to the parent (Client). Using CascadeType.ALL can cause deleting an Invoice to delete its Client, which is usually unintended. Typically, cascading is set from the one side to the many side.