I have the entity

@Entity
public class MyEntity {

    @Id
    Long id;

    @Column
    @Enumerated(EnumType.STRING)
    Status status;

    @Column
    Integer value;

    public Status getStatus() {
        return status;
    }

    public Integer getValue() {
        return value;
    }

}

public enum Status {
   CREATED, DELETED
}

And there is a JpaRepository:

public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {}

Now, there is a test case (outside of any transaction):

MyEntity entity = doSomeBusinesLogic();
entity = myEntityRepository.getOne(entity.getId());
assertEquals(1, entity.getValue()); // Works fine
assertEquals(Status.CREATED, entity.getStatus()); // Throws exception

So the access to the Integer works fine, but the access to the enum throws an org.hibernate.LazyInitializationException.

I got this error after migrating from Spring Boot 1.5 (with Hibernate 5.0) to Spring Boot 2.0 (with Hibernate 5.2).

One working solution is to do the above code inside of an read-only-transaction. But I need to understand why? And why have that changed from Spring Boot 1.5 to 2.0?

Any ideas? TIA!

0 Answers