Lazy loading in TomEE is not working as expected

194 Views Asked by At

Entity's lazy property is always returning null value in TomEE, but working in Glassfish 3 as expected. Is there any listener i am missing to include in web.xml or something else? How to fetch lazy property?

Here is the source code:

AppGroup.java:

package uz.mf.javaee6app;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class AppGroup implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }
}

AppUser.java: package uz.mf.javaee6app;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 *
 * @author ravshan
 */
@Entity
public class AppUser implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private List<AppGroup> roles;

    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public List<AppGroup> getRoles() { return roles; }

    public void setRoles(List<AppGroup> roles) { this.roles = roles; }
}

I'm skipping AppUserFacade stateless bean and UserManager CDI bean, there's nothing special. and the last users.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Selected user: #{userManager.selectedUser.name}
        <h:dataTable value="#{userManager.selectedUser.roles}" var="role">
            <h:column>#{role.name}</h:column>
        </h:dataTable>
    </h:body>
</html>

UserManager.java: package uz.mf.javaee6app;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class UserManager implements Serializable {

    private AppUser selectedUser;

    public AppUser getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(AppUser selectedUser) {
        this.selectedUser = selectedUser;
    }

}

Selected user is being set by another view.

1

There are 1 best solutions below

1
On BEST ANSWER

I'd bet the problem is that somehow your Entity is getting detached prior to accessing the lazy loaded field. Be sure to check your transaction attributes to ensure that your context is still active when you are trying to get lazy loading working.