Transactions
------------

Q1: Does the implementation in CustomerService fulfill the requirements from the TransactionTest
givenInvalidCustomerInformation_whenCreateCustomerWithNewLicensesAndExceptionIsThrown_thenNothingIsCreated?

Answer: Without the @Transactional annotation, the implementation would not fulfill the requirements. Each repository operation would run in its own transaction, meaning that the driving licenses would be committed to the database before the exception is thrown when checking if the customer already exists. With @Transactional present, the entire method runs in one transaction and if an exception is thrown, all changes (including the saved licenses) are rolled back, which is the correct behavior.

Q2: Why does update in the LessonService not generate an update query?

Answer: The update query is not generated because there is no @Transactional annotation on the update method. Without a transaction, the persistence context is never flushed to the database. The entity is retrieved and modified in the persistence context, but since there's no transaction boundary, the changes are never committed to the database. Adding @Transactional ensures the persistence context is flushed at the end of the method, generating the UPDATE SQL statement.

Transaction Isolation Levels - Dirty Reads
------------------------------------------

Q3: Why does the experiment not work?

Answer: The dirty read experiment doesn't work because PostgreSQL does not support the READ_UNCOMMITTED isolation level. When you specify Isolation.READ_UNCOMMITTED in PostgreSQL, it automatically upgrades it to READ_COMMITTED, which prevents dirty reads. PostgreSQL only supports READ_COMMITTED, REPEATABLE_READ, and SERIALIZABLE isolation levels. Therefore, the second transaction cannot read the uncommitted "Nonsensical Name" before it's rolled back - it will only see committed data.

Entity Lifecycle and Cascades
-----------------------------

Q4: Why are lesson and subject not both saved at the same time? Why does the method crash?

Answer: The method crashes because the Lesson entity has a ManyToOne relationship with Subject, but there is no cascade configuration (specifically CascadeType.PERSIST) on that relationship. When you try to save a Lesson with a new Subject that hasn't been persisted yet, JPA will attempt to save the Lesson but the Subject is in a transient/detached state. Without CASCADE.PERSIST, JPA doesn't automatically persist the Subject first, resulting in an error about trying to save an entity with a reference to a transient entity. To fix this, you need to either manually save the Subject first, or add cascade = CascadeType.PERSIST to the @ManyToOne annotation.

Q5: Why does the delete operation no longer work when all the cascades are present?

Answer: The delete operation fails when CASCADE.ALL (or CASCADE.REMOVE) is present on all relationships because deleting a Lesson would cascade the delete to all its associated entities (Subject, Classroom, Employee, Vehicle). However, these associated entities are typically shared across multiple lessons - they have a ManyToOne relationship from Lesson's perspective. When JPA tries to delete these shared entities due to the cascade, it violates foreign key constraints because other Lessons still reference them. For example, if Lesson 6 uses Subject 4, and you delete Lesson 6 with CASCADE.ALL, it would try to delete Subject 4, but other lessons might also reference Subject 4, causing a foreign key constraint violation. CASCADE.REMOVE should generally not be used on ManyToOne relationships where the "One" side is shared.
