2012-12-02 58 views
1

我有一个实体,有一些懒加载的属性,我试图初始化一个特定的方法(大部分时间它太昂贵,这些被加载,所以他们不能被设置为EAGER)。Hibernate.initialize()被完全忽略

其中一个属性是一个集合,它在调用Hibernate.initialize()时加载得很漂亮。另一个是单个对象,不管我尝试什么都不会加载。我甚至不能查询它,因为对象的ID本身不可用,即使在同一个会话中也是如此。

我在调用initialize()时没有收到任何异常,它只是简单地被忽略。该日志在集合上调用此查询时会显示相应的查询,但不会显示在单个对象上。加载此属性(设备)的任何帮助,非常感谢。这是代码:

编辑 事实证明,我在数据库中缺少一个外键。一旦我纠正了这个问题,控制台上就会出现提取查询,但是在initialize()完成后Device对象仍然是一个代理。我想出了一个解决方法做这个:

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; 
    } 
} 

回答

1

显然你的代码已经工作的,因为你可以在代理访问场“名”。为什么您需要将代理替换为基础对象?在hibernate文档中我没有发现任何提示,意味着初始化不仅仅是将真实对象放在代理之后。

Hibernate可能不会放弃代理以保持对它的引用有效。您可以按照注释部分中的链接所述从代理获取实施。

+0

因为我返回的对象将被序列化为JSON,并且为了能够访问这些属性的数据,我需要手动将它复制到另一个对象。出于某种原因,这在集合上的作用是不同的。在这种情况下,初始化会自动加载对象的属性,从而消除克隆对象的繁琐。 – JayPea

+0

使用反射完成了JSON转换吗?尝试一种接口实现方法。 Hibernate总是使用代理来实现懒惰的一对一关系。对于一对多,它可以使用自定义集合类。 – sorencito

+1

https://forum.hibernate.org/viewtopic.php?t=947035 – sorencito