0

我目前正在开发由 Java Web 应用程序和 C# 客户端应用程序组成的系统。Web 应用程序具有 Java Web Service,它具有返回Program类的实体对象的方法:

@WebMethod(operationName = "getProgram")
public Program getProgram(@WebParam(name = "macAddress") String macAddress){
    Device device = DeviceManager.getInstance().getDevice(macAddress);
    if(device != null){
        return device.getProgram();
    }
    return null;
}

这个Program类型的返回对象具有许多属性和关系:

@Entity
@Table(name = "PROGRAM", schema = "APP")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Program.getProgramsByWeather", query = "SELECT p FROM Program p WHERE p.weather = :weather")})
public class Program extends DbEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @JoinColumn(name = "LOGO_ID", referencedColumnName = "ID")
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch= FetchType.EAGER)
    private Logo logo;
    @JoinColumn(name = "WEATHER_ID", referencedColumnName = "ID")
    @ManyToOne
    private Weather weather;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "program", orphanRemoval = true)
    private List<ProgramPlaylist> programPlaylistList = new ArrayList<>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "program", orphanRemoval = true)
    private List<ProgramTicker> programTickerList = new ArrayList<>();
    @Column(name = "UPDATED")
    private boolean updated;

    public Program() {
    }

    public Program(String name, AppUser owner) {
        super(name, owner);
    }

    public Logo getLogo() {
        return logo;
    }

    public void setLogo(Logo logo) {
        this.logo = logo;
    }

    public Weather getWeather() {
        return weather;
    }

    public void setWeather(Weather weather) {
        this.weather = weather;
    }

    public boolean isUpdated() {
        return updated;
    }

    public void setUpdated(boolean updated) {
        this.updated = updated;
    }

    @XmlElement
    public List<ProgramPlaylist> getProgramPlaylistList() {
        return programPlaylistList;
    }

    @XmlElement
    public List<ProgramTicker> getProgramTickerList() {
        return programTickerList;
    }

    @Override
    public String toString() {
        return "Program[ id=" + getId() + " ]";
    }
}

客户端可以获取此对象并访问客户端应用程序中的某些属性,例如它从DbEntity继承的 program.name ,但是当我尝试调用这样的东西时:

program.logo.name

客户端抛出 NullReferenceException。当我尝试迭代 programPlaylistList ArrayList 的元素时,也会发生同样的异常。

我假设传递给客户端的对象本身没有完全加载。

我该如何解决这个问题,请帮忙?!

编辑 好的,所以我打印了客户端从服务中获得的 XML 响应,并且它被正确填充,但由于某种原因,对象字段没有被填充并且大部分为空。为什么会出现这种情况?

4

1 回答 1

0

默认情况下,@OneToMany 注释的获取策略是LAZY,您是否尝试过像在 @oneToOne 字段(fetch= FetchType.EAGER)中那样将其指定为 EAGER?

于 2013-04-25T22:07:19.190 回答