Announcement Announcement Module
Collapse
No announcement yet.
LazyLoading with spring and hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyLoading with spring and hibernate

    Hi,
    I'm trying to solve my LazyLoading problem, which I worked around by fetchType EAGER in the past. But that cannot be the final solution.

    I tried to construct an example, which does not make much sence the way it is, but demonstrates my problem: I have a company, and persons are @ManyToOne by composition. The first time I launch the page everything works fine. The personCount is =0 displayed.
    Now I select a company, which should result in some rerendering for Person inputs etc.
    But when the code tries to access the personList by a calculator, I get this exception:
    Code:
    Schwerwiegend [javax.enterprise.resource.webcontainer.jsf.context] (http--127.0.0.1-8080-4) javax.el.ELException: /tableCompanies.xhtml @31,76 value=" #{calculator.getPersonCount(_var)}":
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: business.domain.Company.personList, no session or session was closed
    Maybe someone can help me solving this?

    tableCompanies.xhtml:
    Code:
    <p:dataTable var="_var" value="#{facade.companies}">
    <p:column>
    <h:outputText value="#{calculator.getPersonCount(_var)}" />
    backing facades:
    Code:
    @Named
    @RequestScoped
    class Facade() {
        @Inject
        Dao dao;
    
        List<Company> companies;
    
        @PostConstruct
        init() {
            companies = Dao.findByNamedQuery("Companies.ALL");
        }
    }
    
    
    @Named
    @RequestScoped
    class Calculator {
        int getPersonCount(Company c) {
            return c.getPersonList().size(); //EX
        }
    }

    Crud service:
    Code:
    @Stateless
    @Transactional
    class Dao() {
        @PersistenceContext
        private EntityManager em;
    
        //CRUD
    }
    Entity:
    Code:
    @Entity
    @NamedQueries( {
        @NamedQuery(name = Company.ALL",
                    query = "SELECT c FROM Company c")
    })
    class Company {
        @OneToMany(cascade = CascadeType.ALL)  // fetch=FetchType.EAGER <-ugly, but would work
        List<Person> personList = new LinkedList<Person>();
    }
    also already configured the Spring OpenEntityManager in web.xml:
    Code:
    <filter>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <filter-class>
            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
        <init-param>
            <param-name>entityManagerFactoryBeanName</param-name>
            <param-value>entityManagerFactory</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    Nevertheless it does not work. But I have not more idea why! Maybe someone knows more?

    tyvm

  • #2
    Hi, you can define the relationship in the other side:

    Code:
    @Entity
    public class Person {
    @ManyToOne(cascade={CascadeType.ALL}) private Company company; ...
    }
    And then rewrite (with Spring Data JPA)
    Code:
    int getPersonCount(Company c) {
            return repository.findByCompany(c).size();
    }

    Comment

    Working...
    X