I'm trying to setup my hibernate mappings to load with a distinct behavior in mind. I would like all properties, which are not Collections/Explicit Proxies, to load immediately and have the remainder collections load when I call Hibernate.initialize. I figured I could manage this by setting lazy=false and overiding this setting to lazy for all collections. For example:
Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = false)
@Table(name = "OBJECT_T")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SimObject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PERSIST_ID", nullable = false)
private Long id = new Long(0);
@Column(name = "PERSIST_LOG_TIME", nullable = false)
private Date logTime = new Date();
@Column(name = "CONFIG_NAME")
private String configName = "";
@ManyToOne(fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "EQUIP_INFO_FK")
private SimEquipmentInfo info = null;
@OneToMany(fetch = FetchType.LAZY)
@Cascade({CascadeType.DELETE, CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE,CascadeType.REFRESH, CascadeType.DETACH})
@JoinTable(name = "SIM_TEST_MAP_T", joinColumns = {
@JoinColumn(name = "TEST_ID")
}, inverseJoinColumns = {
@JoinColumn(name = OTHER_ID")
})
private final Set<SimTest> test = new HashSet<SimTest>(0);
...
To retrieve initial resultset:
Code:
Query query = hsession.createQuery(strHQL);
results = query.list();
Now,, when I need further data, the collections, just call:
Code:
Hibernate.initialize(o);
Note, this is of course done with the session open.Now, with these settings in mind, I call Hibernate.initialize(object) and the equipment info and test collection don't initialize. I've read through the doc,
http://docs.jboss.org/hibernate/core/4. ... ialization, but it doesn't seem very clear how the 'initialize' method operates under different fetching strategies. It's also not very clear how the 'initiailize' method loads parent/child relationships. After reading the doc, it seems my understand is initialize should load all parent/child data. However, this is not the case for the example above. None of the lazy properties get loaded. I understand I could drill down and call Hibernate.Initialize(parent.getChild()), but why would I even use initialize for that when the getter will fetch the data automatically. What exactly is the advantage of this Hibernate.Initialize method?
What I would really like to achieve here is having properties load by default with the exception of collections and any proxy objects i flag 'lazy'. I don't have the luxury of keeping long sessions open here, nor do I want to continually call n+1 queries everytime i hit a getter method. I can reattach/merge if needed, but I don't want a ton of long sessions open throughout the life of my program. Any info/advise for this use-case is greatly appreciated!