Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 完全忽略Hibernate.initialize()_Java_Hibernate_Lazy Loading - Fatal编程技术网

Java 完全忽略Hibernate.initialize()

Java 完全忽略Hibernate.initialize(),java,hibernate,lazy-loading,Java,Hibernate,Lazy Loading,我有一个实体,它有一些延迟加载的属性,我正试图为一个特定的方法初始化这些属性(大多数情况下,加载这些属性的代价太高,所以不能将它们设置为“渴望”) 其中一个属性是集合,当调用Hibernate.initialize()时,它可以很好地加载。另一个是单个对象,无论我尝试什么,它都不会加载。我甚至无法查询它,因为对象的id本身不可用,即使在同一会话期间也不可用 调用initialize()时没有任何异常,只是被忽略了。当对集合而不是单个对象调用此函数时,日志将显示相应的查询。非常感谢您对加载此属性(

我有一个实体,它有一些延迟加载的属性,我正试图为一个特定的方法初始化这些属性(大多数情况下,加载这些属性的代价太高,所以不能将它们设置为“渴望”)

其中一个属性是集合,当调用Hibernate.initialize()时,它可以很好地加载。另一个是单个对象,无论我尝试什么,它都不会加载。我甚至无法查询它,因为对象的id本身不可用,即使在同一会话期间也不可用

调用initialize()时没有任何异常,只是被忽略了。当对集合而不是单个对象调用此函数时,日志将显示相应的查询。非常感谢您对加载此属性(设备)的任何帮助。代码如下:

编辑 结果发现我的数据库中缺少一个外键。一旦我纠正了这一点,fetch查询就会出现在控制台上,但是,initialize()完成后,设备对象仍然是一个代理。我通过这样做想出了一个解决办法:

Device device = new Device();
        device.setIdDevice(surveyLog.getDevice().getIdDevice());
        device.setName(surveyLog.getDevice().getName());
        surveyLog.setDevice(device);
如果将属性复制到另一个对象,则可以在方法外部访问这些属性,否则该对象仍将是代理。你知道这是什么原因吗?谢谢

@Transactional    
public SurveyLog getById(Long id)
{
    SurveyLog surveyLog = this.surveyLogDAO.findById(id);

    Hibernate.initialize(surveyLog.getDevice()); //doesn't work 
    Hibernate.initialize(surveyLog.getSurveyAnswers()); //works like a charm
    return surveyLog;
}
这些是我的映射:

import static javax.persistence.GenerationType.IDENTITY;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.google.gson.annotations.Expose;

@Entity
@Table(name="SurveyLog")
public class SurveyLog
{
    // Fields    
    @Expose private Long idSurveyLog;
    @Expose private Survey survey;
    @Expose private Device device;
    @Expose private BigDecimal latitude;
    @Expose private BigDecimal longitude;
    @Expose private Timestamp timestamp;
    @Expose private List<SurveyAnswerLog> surveyAnswers = new ArrayList<SurveyAnswerLog>();

    /**
     * Constructor.
     * 
     */
    public SurveyLog() {}

    // Property accessors
    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="idSurveyLog", unique=true, nullable=false)
    public Long getIdSurveyLog()
    {
        return idSurveyLog;
    }

    public void setIdSurveyLog(Long idSurveyLog)
    {
        this.idSurveyLog = idSurveyLog;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="idSurvey", nullable=false)
    public Survey getSurvey()
    {
        return survey;
    }

    public void setSurvey(Survey survey)
    {
        this.survey = survey;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @Fetch(value = FetchMode.SELECT)
    @JoinColumn(name="idDevice", nullable=false)    
    public Device getDevice()
    {
        return device;
    }

    public void setDevice(Device device)
    {
        this.device = device;
    }

    @Column(name="latitude", precision=8, scale=6)
    public BigDecimal getLatitude()
    {
        return latitude;
    }

    public void setLatitude(BigDecimal latitude)
    {
        this.latitude = latitude;
    }

    @Column(name="longitude", precision=9, scale=6)
    public BigDecimal getLongitude()
    {
        return longitude;
    }

    public void setLongitude(BigDecimal longitude)
    {
        this.longitude = longitude;
    }

    @Column(name="timestamp", length=19)
    public Timestamp getTimestamp()
    {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp)
    {
        this.timestamp = timestamp;
    }

    @OneToMany(fetch=FetchType.LAZY, mappedBy="surveyLog")
    @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    public List<SurveyAnswerLog> getSurveyAnswers()
    {
        return surveyAnswers;
    }

    public void setSurveyAnswers(List<SurveyAnswerLog> surveyAnswers)
    {
        this.surveyAnswers = surveyAnswers;
    }
}



import static javax.persistence.GenerationType.IDENTITY;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

import com.google.gson.annotations.Expose;
import com.sitei.base.model.City;
import com.sitei.base.model.Company;

@Entity
@Table(name="Device", uniqueConstraints = @UniqueConstraint(columnNames = "macAddress"))
public class Device
{
    // Fields
    @Expose private Integer idDevice;
    @Expose private String macAddress;
    @Expose private String name;
    @Expose private String description;
    @Expose private Short type;
    @Expose private City city;
    @Expose private Company company;
    @Expose private Survey survey;
    @Expose private Integer surveyPoints;
    @Expose private boolean active;
    @Expose private Long version;



    /**
     * Constructor.
     * 
     */
    public Device() {}

    // Property accessors
    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="idDevice", unique=true, nullable=false)
    public Integer getIdDevice()
    {
        return idDevice;
    }

    public void setIdDevice(Integer idDevice)
    {
        this.idDevice = idDevice;
    }

    @Column(name="macAddress", nullable=false, length=17)
    public String getMacAddress()
    {
        return macAddress;
    }

    public void setMacAddress(String macAddress)
    {
        this.macAddress = macAddress;
    }

    @Column(name="name", nullable=false, length=64)
    public String getName()
    {
        return name;
    }

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

    @Column(name="description", length=256)
    public String getDescription() 
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    @Column(name="type", nullable=false)
    public Short getType()
    {
        return type;
    }

    public void setType(Short type)
    {
        this.type = type;
    }

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="idCity", nullable=false)
    public City getCity()
    {
        return city;
    }

    public void setCity(City city)
    {
        this.city = city;
    }

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="idCompany")
    public Company getCompany()
    {
        return company;
    }

    public void setCompany(Company company)
    {
        this.company = company;
    }

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="idSurvey")
    public Survey getSurvey()
    {
        return survey;
    }

    public void setSurvey(Survey survey)
    {
        this.survey = survey;
    }

    @Column(name="surveyPoints", nullable=false)
    public Integer getSurveyPoints()
    {
        return surveyPoints;
    }

    public void setSurveyPoints(Integer surveyPoints)
    {
        this.surveyPoints = surveyPoints;
    }

    @Column(name="active", nullable=false)
    public boolean isActive()
    {
        return active;
    }

    public void setActive(boolean active)
    {
        this.active = active;
    }

    @Version
    public Long getVersion()
    {
        return version;
    }

    public void setVersion(Long version)
    {
        this.version = version;
    }
}
导入静态javax.persistence.GenerationType.IDENTITY;
导入java.math.BigDecimal;
导入java.sql.Timestamp;
导入java.util.ArrayList;
导入java.util.List;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.manytone;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
导入org.hibernate.annotations.Cascade;
导入org.hibernate.annotations.CascadeType;
导入org.hibernate.annotations.Fetch;
导入org.hibernate.annotations.FetchMode;
导入com.google.gson.annotations.Expose;
@实体
@表(name=“SurveyLog”)
公共类调查
{
//田地
@公开私有长idSurveyLog;
@公开私人调查;
@公开私有设备;
@公开私人信息;
@公开私有数据;
@公开私有时间戳;
@Expose private List surveyAnswers=new ArrayList();
/**
*构造器。
* 
*/
公共调查日志(){}
//属性访问器
@身份证
@生成值(策略=标识)
@列(name=“idSurveyLog”,unique=true,nullable=false)
公共长getIdSurveyLog()
{
返回idSurveyLog;
}
公共无效集合idSurveyLog(长idSurveyLog)
{
this.idSurveyLog=idSurveyLog;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“idSurvey”,nullable=false)
公众调查
{
回归调查;
}
公共调查(调查)
{
这个调查=调查;
}
@manytone(fetch=FetchType.LAZY)
@获取(值=获取模式。选择)
@JoinColumn(name=“idDevice”,nullable=false)
公共设备getDevice()
{
返回装置;
}
公共无效设置设备(设备)
{
这个装置=装置;
}
@列(名称=“纬度”,精度=8,刻度=6)
公共BigDecimal getLatitude()
{
返回纬度;
}
公共void设置纬度(BigDecimal纬度)
{
这个。纬度=纬度;
}
@列(名称=“经度”,精度=9,刻度=6)
公共bigdecim getLongitude()
{
返回经度;
}
公共无效设置经度(BigDecimal经度)
{
这个经度=经度;
}
@列(name=“timestamp”,长度=19)
公共时间戳getTimestamp()
{
返回时间戳;
}
public void setTimestamp(Timestamp Timestamp)
{
this.timestamp=时间戳;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“surveyLog”)
@级联({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
公共列表getSurveyAnswers()
{
返回调查和回答;
}
公共无效设置调查和回答者(列表调查和回答者)
{
this.surveyAnswers=surveyAnswers;
}
}
导入静态javax.persistence.GenerationType.IDENTITY;
导入java.util.EnumSet;
导入java.util.HashMap;
导入java.util.Map;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.manytone;
导入javax.persistence.Table;
导入javax.persistence.UniqueConstraint;
导入javax.persistence.Version;
导入com.google.gson.annotations.Expose;
导入com.sitei.base.model.City;
导入com.sitei.base.model.Company;
@实体
@表(name=“Device”,uniqueConstraints=@UniqueConstraint(columnNames=“macAddress”))
公共类设备
{
//田地
@公开私有整数设备;
@公开私有字符串macAddress;
@公开私有字符串名称;
@公开私有字符串描述;
@公开私人短型;
@揭露私人城市;
@揭露私人公司;
@公开私人调查;
@公开私有整数测量点;
@公开私有布尔活动;
@公开私有长版本;
/**
*构造器。
* 
*/
公用设备(){}
//属性访问器
@身份证
@生成值(策略=标识)
@列(name=“idDevice”,unique=true,nullable=false)
公共整数getIdDevice()
{
返回装置;
}
public void setIdDevice(整数idDevice)
{
this.idDevice=idDevice;
}
@列(name=“macAddress”,null=false,长度=17)
公共字符串getMacAddress()
{
返回macAddress;
}
public void setMacAddress(字符串macAddress)
{
this.macAddress=macAddress;
}
@列(name=“name”,nullable=false,length=64)
公众的
List<String> methodNames= new ArrayList();

methodNames.add("getScTeamUserses");


public T getByID(PK id, List<String> methodNames) {
    T object = null;
    Session sess = getSession();
    try {
        sess.beginTransaction();
        object = (T) sess.get(clazz, id);
        Class[] clazz = null;


        for (String methodName : methodNames) {
            Method method = object.getClass().getMethod(methodName, clazz);                
            Hibernate.initialize(method.invoke(object, clazz));
        }   



        sess.getTransaction().commit();
    } catch (Exception e) {
         logger.error("getByID id=" + id, e);
        try {               
            sess.getTransaction().rollback();
            throw e;
        } catch (Exception ex) {
            logger.error("getByID methodNames id=" + id, e);
        }
    } finally {
        closeSession(sess);
    }
    return object;
}