somebody
somebody

Reputation: 1107

Hibernate LazyInitializingException

For example, I have 2 classes. The first

@Entity
@Table(name = "employee")
public class Employee implements Serializable{

    @ManyToOne
    @JoinColumn(name = "office_address_id")
    private Address address;

    //setters and getters
}

And the second:

@Entity
@Table(name = "address")
public class Address implements Serializable{

    @OneToMany(mappedBy = "address")
    private List<Employee> employeeList;

    //setters and getters
}

So if I wand read Employee from database, I read address field. Address have employeeList with LazyInitializingException. But I don't want to know employeeList. I wanna know only employee.getAddress(). I wand to send JSON object Employee. But at the client-side I have Failed to load resource: the server responded with a status of 500 (Internal Server Error) by reason of LazyInitializingException .

May I use:

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceUnitName" value="myPersistenceUnit"/>
    <property name="packagesToScan" value="com.itechart.model"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="true"/>
        </bean>
    </property>
    <!-- USE--!>
    <property name="jpaProperties">
        <props>
             <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
    <!-- END USE--!>
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
    </property>
</bean>

Upvotes: 0

Views: 78

Answers (2)

user3769706
user3769706

Reputation:

Your JSON mapper might access Address.employeeList after hibernate session is closed. That is why it sends LazyInitializingException.

You can try any of the below solutions to solve the issue.

1 ) Exclude employeeList from the JSON mapping

In Jackson we can do it adding @JsonIgnore

@JsonIgnore
public List<Employee> getEmployeeList() {
}  

2 ) Initialize employeeList before send it to the JSON mapper.

You can do it by calling employeeList.size() method

3 ) You can change the fetchType to eager like below

@OneToMany(mappedBy = "address", fetch = FetchType.EAGER) 
private List<Employee> employeeList;

Upvotes: 1

Stijn Geukens
Stijn Geukens

Reputation: 15628

The problem is that during the JSON mapping it will walk through all the properties and try to access Address.employeeList. When it does you obviuosly are no longer in a Hibernate Session and hence the exception.

Either exclude employeeList from the JSON mapping (clean solution) or initialize employeeList before the session is terminated by changing the fetch type of the OneToMany to EAGER or explicitly initializing the collection (= quick and dirty).

My preferred solution to avoid issues like this is not to pass entities to the frontend code but always map them to TO's (Transfer Objects).

Upvotes: 0

Related Questions