package org.yu55.pwjpa21.demo01;

import javax.persistence.EntityManager;

public class JournalService {

    private EntityManager entityManager;

    JournalService(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    Journal createJournal(Long id, String title, Integer numberOfPages) {
        Journal journal = new Journal(id, title, numberOfPages);
        entityManager.persist(journal);
        return journal;
    }

    Journal findJournal(Long id) {
        return entityManager.find(Journal.class, id);
    }

    Journal updateTitle(Long id, String newTitle) {
        Journal journal = entityManager.find(Journal.class, id);
        if (journal != null) {
            journal.setTitle(newTitle);
        }
        return journal;
    }

    void removeJournal(Long id) {
        Journal journal = entityManager.find(Journal.class, id);
        if (journal != null) {
            entityManager.remove(journal);
        }
    }
}

@@ -250,142 +250,2305 @@
     
-Journal createJournal(Long id, String title, Integer numberOfPages) %7B%0A        Journal journal = new Journal(id, title, numberOfPages);
+/*%0A    Entity life cycle: new, managed, detached, removed.%0A    - A new entity instance has no persistent identity, and is not yet associated with a persistence context.%0A    - A managed entity instance is an instance with a persistent identity that is currently associated with a persistence%0A    context.%0A    - A detached entity instance is an instance with a persistent identity that is not (or no longer) associated with%0A    a persistence context.%0A    - A removed entity instance is an instance with a persistent identity, associated with a persistence context, that%0A    will be removed from the database upon transaction commit.%0A    Use of the cascade annotation element may be used to propagate the effect of an operation to associated entities.%0A     */%0A%0A    Journal createJournal(Long id, String title, Integer numberOfPages) %7B%0A        /*%0A        Entity is created but it's not managed nor persistent yet.%0A         */%0A        Journal journal = new Journal(id, title, numberOfPages);%0A        /*%0A        A new entity instance becomes both managed and persistent by invoking the persist method on it or by cascading%0A        the persist operation.%0A%0A        The semantics of the persist operation, applied to an entity X are as follows:%0A        - If X is a new entity, it becomes managed. The entity X will be entered into the database at or before%0A        transaction commit or as a result of the flush operation.%0A        - If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist operation%0A        is cascaded to entities referenced by X, if the relationships from X to these other entities are annotated with%0A        the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor%0A        element.%0A        - If X is a removed entity, it becomes managed.%0A        - If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or%0A        the EntityExistsException or another PersistenceException may be thrown at flush or commit time.%0A        - For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the%0A        cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.%0A         */
 %0A   

Add JPA specification comments: entity life cycle.