TomEE中的延迟加载未按预期工作

时间:2013-12-03 06:52:10

标签: jpa-2.0 java-ee-6 glassfish-3 openjpa tomee

实体的延迟属性总是在TomEE中返回null值,但是按照预期在Glassfish 3中工作。是否有任何我不想包括在web.xml或其他内容的听众?如何获取懒惰属性?

以下是源代码:

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:的     包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; }
}

我正在跳过 AppUserFacade 无状态bean和 UserManager CDI bean,没什么特别之处。和最后的 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:的     包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;
    }

}

所选用户正在由另一个视图设置。

1 个答案:

答案 0 :(得分:0)

我敢打赌,问题在于,在访问延迟加载字段之前,您的实体会以某种方式分离。请务必检查您的事务属性,以确保在尝试延迟加载时您的上下文仍处于活动状态。