I have an small console application and I am using spring-data-jpa with hibernate. I really can not figure out how to lazy initialize collections when using spring-data-jpa with its repositories, in an standalone console application. Here is some of my code:
@Entity
public class User {
...
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="USER_ORDER_ID")
private Set<Order> orders = new HashSet<Order>();
...
}
repository:
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
public ArrayList<User> findByFirstNameIgnoreCase(String firstName);
}
service impl:
@Service
@Repository
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
public ArrayList<User> findByFirstNameIgnoreCase(String firstName) {
ArrayList<User> users = new ArrayList<User>();
users = userRepository.findByFirstNameIgnoreCase(firstName);
return users;
}
my main method:
...
user = userRepository.findByFirstNameIgnoreCase("john").get(0);
orders = user.getOrders();
for (Order order : orders) {
LOGGER.info("getting orders: " + order.getId());
}
the foreach loop gets an exception:
EVERE: failed to lazily initialize a collection of role: com.aki.util.User.orders, no session or session was closed org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
Please note that I do not have this problem when running this from an webapp with some kind of OpenSessionInViewFilter.