Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate.initialize() being completely ignored
    primarykey
    data
    text
    <p>I have an entity with some lazy loaded properties which I'm trying to initialize for one particular method (most of the time it's too expensive for these to be loaded, so they can't be set to EAGER). </p> <p>One of these properties is a collection and it loads beautifully when calling Hibernate.initialize(). The other is a single object and it won't load no matter what I try. I can't even query for it, since the object's id itself isn't available, not even during the same session. </p> <p>I'm not getting any exceptions when calling initialize(), it's just simply being ignored. The log shows the corresponding query when calling this on the collection but not on the single object. Any help on loading this property (Device) is greatly appreciated. This is the code:</p> <p><strong>EDIT</strong> It turns out I had a missing foreign key in the database. Once I corrected this, the fetch query appeared on the console, however, the Device object is still a proxy after initialize() completes. I came up with a workaround by doing this:</p> <pre><code>Device device = new Device(); device.setIdDevice(surveyLog.getDevice().getIdDevice()); device.setName(surveyLog.getDevice().getName()); surveyLog.setDevice(device); </code></pre> <p>If I copy the properties to another object, they can be accessed outside the method, otherwise the object remains a proxy. Any idea what could be causing this? Thanks</p> <pre><code>@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; } </code></pre> <p>And these are my mappings:</p> <pre><code>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&lt;SurveyAnswerLog&gt; surveyAnswers = new ArrayList&lt;SurveyAnswerLog&gt;(); /** * 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&lt;SurveyAnswerLog&gt; getSurveyAnswers() { return surveyAnswers; } public void setSurveyAnswers(List&lt;SurveyAnswerLog&gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload