Skip to content

Removing Transactional Still Working #14

@tharari21

Description

@tharari21

Hello,

This is not an issue with any of the lectures I simply misunderstood something and need some help. The way I understand @transactional is that it keeps the session (Hibernate) or persistence context (JPA) open and every database change in the method is under one transaction. Also if transactional is not placed on a method, a new persistence context will be created and destroyed for each line of code that accesses entity manager. I wanted to test that this understanding was correct so I took the retrievePassportAndAssociatedStudent test method and removed @transactional. You stated in the course that because fetch type is lazy, the persistence context closes after we call em.find() and therefore the following call to passport.getStudents() should cause a "could not initialize proxy" exception. Well after removing @transactional my code still tested fine with no exceptions and I really cannot understand why. I ensured fetch type is lazy both sides of relationship.

@Test
//    @Transactional
    public void retrievePassportAndAssociatedStudent() {
        Passport passport = em.find(Passport.class, 40001L);
        logger.info("Passport -> {}", passport);
        logger.info("Passport Student-> {}", passport.getStudent());
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions