LazyInitializationException in selectManyCheckbox on @ManyToMany(fetch=LAZY)

You need to fetch it while inside a transaction (thus, inside the service method), and not while outside a transaction (thus, inside e.g. JSF managed bean init/action method), that would thus throw a LazyInitializationException. So, your attempt hardware.getConnectivities().size(); has to take place inside a transaction. Create if necessary a new service method for the purpose … Read more

How to implement thread-safe lazy initialization?

For singletons there is an elegant solution by delegating the task to the JVM code for static initialization. public class Something { private Something() { } private static class LazyHolder { public static final Something INSTANCE = new Something(); } public static Something getInstance() { return LazyHolder.INSTANCE; } } see http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom and this blog post … Read more

Thread safe lazy construction of a singleton in C++

Basically, you’re asking for synchronized creation of a singleton, without using any synchronization (previously-constructed variables). In general, no, this is not possible. You need something available for synchronization. As for your other question, yes, static variables which can be statically initialized (i.e. no runtime code necessary) are guaranteed to be initialized before other code is … Read more

How to solve the LazyInitializationException when using JPA and Hibernate

Hibernate 4.1.6 finally solves this issue: https://hibernate.atlassian.net/browse/HHH-7457 You need to set the hibernate-property hibernate.enable_lazy_load_no_trans=true Here’s how to do it in Spring: <bean id=”entityManagerFactory” class=”org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”> <property name=”dataSource” ref=”myDataSource”/> <property name=”packagesToScan” value=”com.mycompany.somepackage”/> <property name=”jpaVendorAdapter” ref=”hibernateVendorAdapter”/> <property name=”jpaDialect” ref=”jpaDialect”/> <property name=”jpaProperties”> <props> <prop key=”hibernate.enable_lazy_load_no_trans”>true</prop> </props> </property> </bean> Voila; Now you don’t have to worry about LazyInitializationException while navigating … Read more

Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans

The problem with this approach is that you can have the N+1 effect. Imagine that you have the following entity: public class Person{ @OneToMany // default to lazy private List<Order> orderList; } If you have a report that returns 10K of persons, and if in this report you execute the code person.getOrderList() the JPA/Hibernate will … Read more