Note that there are some explanatory texts on larger screens.

plurals
  1. POlazy loading entities in spring with jpa and hibernate
    primarykey
    data
    text
    <p>I'm using Spring with JPA and Hibernate. I've got some DAO-Classes that are annotated with @Repositoy and some Controller-Classes. When I call one of the dao's methods in my controller to load some entities I get back the Entity and after that, I want to get some other entities that are stored in a field of the first loaded entity. But at that time spring has already closed the session and lazy loading is no longer possible.</p> <p>My db.xml configuration:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-autowire="byName"&gt; &lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt; &lt;bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:db.properties" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;!-- &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="jpaVendorAdapter" /&gt; &lt;/bean&gt; --&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="databasePlatform" value="${db.dialect}" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value="${db.driver}" /&gt; &lt;property name="url" value="${db.url}" /&gt; &lt;property name="username" value="${db.username}" /&gt; &lt;property name="password" value="${db.password}" /&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" /&gt; &lt;/beans&gt; </code></pre> <p>The method in the Dao is annotated as follows:</p> <pre><code>@Transactional(readOnly = true, propagation=Propagation.REQUIRED) </code></pre> <p>now I wanna do something like this:</p> <pre><code>@Controller public class HomeController{ @Autowired private UserDao userDao; @RequestMapping(value = "/", method = RequestMethod.GET) public ResponseEntity&lt;String&gt; home(){ ... User user = userDao.findUser(id); Set&lt;Order&gt; orders = user.getOrders(); ... String myResult = ...; return jsonService.generateResponse(myResult); } } @Repository public class UserDao{ @PersistenceContext private EntityManager entityManager; public User findUser(Integer id){ return entityManager.find(User.class, id); } } </code></pre> <p>The set of orders should be lazy-loaded but i get the following exception: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:...,no session or session was closed</p> <p>root cause: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., no session or session was closed</p> <p>I tried to annotate the method in Controller wirt @Transactional and also to set mode="aspectj" in annotation-driven property in the db.xml, but nothing did work. Is there any way to lazy load the orders of the user?</p> <p>For any help, thanking you in anticipation!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload